<?php

namespace control;
// 业务凭证
class businessVoucherClass extends adIndexClass
{
    private const page = 'businessVoucher';
    private const title = '业务凭证';
    private array $accountReceipt = ['收款单', '付款单'];//账户在明细上的
    private array $accountMainReceipt = ['收入单', '费用单'];//账户在主表上的

    /**
     * 字段初始化
     */
    public function fieldInit($fun): array
    {
        switch ($fun) {
            case 'hexiaodan':
            case 'jiezhuanchukuchengben':
            case 'chengbendiaozhengdan':
            case 'qitachukudan':
            case 'qitarukudan':
            case 'shourudan':
            case 'feiyongdan':
            case 'fukuandan':
            case 'shoukuandan':
            case 'caigouzangudan':
            case 'caigoutuihuo':
            case 'caigouruku':
            case 'xiaoshouchuku':
            case 'xiaoshoutuihuo':
                $tr = [
                    'recordDate'          => [
                        'name'  => "日期",
                        'width' => 120,
                        'sort' => 1,
                    ],
                    'orderNo'  => [
                        'name'  => '单据编号',
                        'width' => 200,
                        'sort' => 1,
                    ],
                    'type'         => [
                        'name'  => '单据类型',
                        'width' => 150,
                    ],
                    'companyName'         => [
                        'name'  => '往来单位',
                        'width' => 200,
                    ],
                    'money'          => [
                        'name'  => '单据金额',
                        'width' => 150,
                    ],
                    'stid'          => [
                        'name'  => '制单人',
                        'width' => 200,
                    ],
                    'url'          => [
                        'name'  => '凭证字号',
                        'width' => 200,

                    ]
                ];
                break;
            case 'xiaoxiangfapiao':
            case 'jinxiangfapiao':
                $tr = [
                    'invoiceDate'          => [
                        'name'  => "开票日期",
                        'width' => 120,
                        'sort' => 1,
                    ],
                    'invoiceNo'  => [
                        'name'  => '发票编号',
                        'width' => 200,
                        'sort' => 1,
                    ],
                    'type'         => [
                        'name'  => '单据类型',
                        'width' => 100,
                    ],
                    'companyName'         => [
                        'name'  => '往来单位',
                        'width' => 200,
                    ],
                    'money'          => [
                        'name'  => '价税合计',
                        'width' => 150,
                    ],
                    'unTaxMoney'          => [
                        'name'  => '未税金额',
                        'width' => 150,
                    ],
                    'taxMoney'          => [
                        'name'  => '税额',
                        'width' => 150,
                    ],
                    'url'          => [
                        'name'  => '凭证字号',
                        'width' => 150,

                    ]
                ];
                break;
            default:
                $tr = [
                    'recordDate'          => [
                        'name'  => "日期",
                        'width' => 200,
                        'sort' => 1,
                    ],
                    'orderNo'  => [
                        'name'  => '单据编号',
                        'width' => 200,
                        'sort' => 1,
                    ],
                    'type'         => [
                        'name'  => '单据类型',
                        'width' => 200,
                    ],
                    'money'          => [
                        'name'  => '单据金额',
                        'width' => 200,
                    ],
                    'stid'          => [
                        'name'  => '制单人',
                        'width' => 200,
                    ],
                    'url'          => [
                        'name'  => '凭证字号',
                        'width' => 200,

                    ]
                ];
        }
        return $tr;
    }

    /*编码入口页*/
    public function home(): string
    {
        $all = $this->getPowerAll();
        //返回
        $rightHtml = $this->homeRight();
        $id = $this->get('id')?:'采购入库';
        $html = componentClass::leftNav([
            'title'=>'类型',
            'all'=>$all,
            'rightHtml'=>$rightHtml,
        ]);
        $html .= <<<EOT
<script>
function replaceRightDetail(obj, cla = '{$this->get('cla')}') {
     let api, id = $(obj).attr('data-id')
    if (id.length > 0) {
        let that = $(obj).parent('ul')
        const two = that.attr('pid');
        const one = that.parent('ul').attr('pid');
        // ulScal(obj, id)
        $('#container-left').find('li').removeClass('active')
        $(obj).addClass('active')
        api = root + 'control/adIndex.html?cla=' + cla + '&fun=homeRightJson'
        $.post(api, {
            menuOne:one,
            menuTwo: two,
            menuThree: id
        }, function (data) {
            $('#container-detail').html(data.html)
        }, "json");
    }
}
 function cancelPage(id){
    let obj =  $("[data-id='"+id+"']");
    replaceRightDetail(obj,'businessVoucher');
}
 $(function (){
     cancelPage('$id')
 })
</script>
EOT;
        //返回
        return $this->homeFun($html);
    }

    /*列表页-查询条件*/
    public function homeOptions($isAsync = true)
    {
        $array = [
            [
                [
                    "单据编号" => text(array("name" => "orderNo")).
                        hidden(['name' => 'menuTwo', 'value' => $this->post('menuTwo')]) .
                        hidden(['name' => 'menuThree', 'value' => $this->post('menuThree')]),
                    "客户名称"   => selectMultiple(["name" => "khid", 'table' => " kehu", 'sqlId' => 'khid', 'where' => "coid = '" . self::$coid . "'", 'sqlField' => 'companyName', 'class' => 'width200']),
                    '供应商' => selectMultiple(["name" => "supplierId", 'table' => "supplier", 'sqlId' => 'id', 'where' => "coid = '" . self::$coid . "'", 'sqlField' => 'name', 'class' => 'width200']),
                ],
                [
                    '生产线'  => selectMultiple(["name" => "lineId", 'table' => "product_line", 'sqlId' => 'id', 'where' => "coid = '" . self::$coid . "'", 'sqlField' => 'name', 'class' => 'width200']),
                    "单据日期" => text(array("name" => "createTime", 'laydate' => ['type' => 'date', 'range' => '~'])),
                ]
            ]
        ];
        if (in_array($this->post('menuThree'), ['收入单', '费用单'])) {
            $array[0][1]['类型'] = select(['name' => 'type', 'option' => ['客户', '供应商']]);
        }
        //生成查询表单
        $html = $this->tableShow($array,1);
        //返回
        if (!$isAsync) {
            return $html;
        } else {
            return json_encode(['html' => $html], 320);
        }
    }

    public function homeRight(): string
    {
        $html = $this->homeOptions(false);
        //控件
        $button = "";
        if ($this->power(self::page, "edit")) {
            $button .= "<span onclick=\"voucher()\" class='spanButton'>生成凭证</span>\n";
        }
        if ($this->power(self::page, "del")) {
            $button .= "<span class='spanButton' onclick=\"layer('{$this->get('cla')}', 'operate', {'act': 'del'})\">删除凭证</span>\n";
        }
        return $this->search($html, layout: 'pop', toolButton: $button);
    }

    /**
     * 左侧搜索框
     *
     * @return string
     */
    public function getContainerLeftJson(): string
    {
        $json['html'] = $this->getPowerAll();
        return json_encode($json, 320);
    }


    public function getPowerAll(array $local = [], string $pid = ''): string
    {
        $obj = new businessVoucherSettingClass();
        $result = $local ?: $obj->templateField();
        $style = empty($pid) ? 'pid=0' : ' class="child-node"';
        $temp = "<ul $style $pid>";
        // 角标
        $arrow = "<span class='arrow'  onclick=\"liZoom($(this).parent())\"></span>";
        $lastone = lastone;
        foreach ($result as $key => $value) {
            // 判断下级是否存在
            $pid = "pid=$key";
            if (is_array($value) && !empty($value)) {
                $temp .= "<li data-id='$key' onclick=\"replaceRightDetail(this)\">$arrow <span class='textcontent'>$key</span></li>";
                $temp .= $this->getPowerAll($value, $pid);
            } else {
                if (is_array($value)) {
                    $name = $key;
                } else {
                    $name = $value;
                    $key = $value;
                }
                $temp .= "<li data-id='$key' onclick=\"replaceRightDetail(this)\">$lastone <span class='textcontent'>$name</span></li>";
            }
        }
        $temp .= '</ul>';
        return $temp;
    }

    /**
     * 异步加载
     */
    public function homeRightJson(): string
    {
        $json['html'] = $this->homeRight();
        return json_encode($json, 320);
    }

    public function homeRightJsonSearch(): string
    {
        return $this->homeSearch();
    }

    /**
     * 获取业务类型
     * @return array
     */
    public function getMenuOne(): array
    {
        $result = [];
        foreach ((new businessVoucherSettingClass())->templateField() as $name => $menu) {
            $result[] = $name;
        }
        return $result;
    }

    /*列表页-查询结果*/
    public function homeSearch(): bool|string
    {
        $secMenuName = $this->post('menuThree');
        $menuOne = $this->post('menuTwo');
        $oneArray = $this->getMenuOne();
        $json['html'] = '';
        foreach ($oneArray as $orderName) {
            if ($orderName == $menuOne || $orderName == $secMenuName) {
                if ($orderName == $secMenuName) {
                    //只有一级的
                    $menuOne = $secMenuName;
                }

                $method = PYClass::encode($orderName, 'all', false, '');
                $json['html'] = $this->$method($menuOne, $secMenuName);
                break;
            }
        }
        return json_encode($json, 320);
    }


    /**
     *
     * 保存凭证
     * @return false|string
     */
    public function voucherEdit()
    {
        $typeOne = $this->post('typeOne');//一级类别
        $typeTwo = $this->post('typeTwo');//二级类别
        $listId = $this->post('listId');
        $mergeVoucher = $this->post('mergeVoucher');
        if (!$this->power(self::page, 'edit')) {
            $json['warn'] = '您还没有编辑权限';
        } elseif (empty($typeOne) || empty($typeTwo)) {
            $json['warn'] = '未找到类型';
        } elseif (empty($listId)) {
            $json['warn'] = '未找到Id';
        } else {
            if ($mergeVoucher) {
                $json = $this->editVoucherMerge($listId, $typeOne, $typeTwo);
            } else {
                $json = $this->editVoucher($listId, $typeOne, $typeTwo);
            }
        }
        return json_encode($json, 320);
    }

    /**
     * 不合并生成
     */
    public function editVoucher($listId, $typeOne, $typeTwo)
    {
        $suc = 0;
        $listArr = explode(',', $listId);
        $text = '';

        foreach ($listArr as $orderId) {
            $children['array'] = [];
            switch ($typeTwo) {
//                case '领料单':
//                case '退料单':
//                    $bookkeeping = query('pick_order', "id = '$orderId'");
//                    $children = find(array('table' => 'pick_order_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
//                    $total = find(array('table' => 'pick_order_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money"));
//                    $orderMoney = self::formattingFigure($total['money']);
//                    $moneyWithNoTax = self::formattingFigure($total['money']);
//                    $taxMoney = 0;
//                    $tableName = 'pick_order';
//                    break;
                case '采购入库':
                    $bookkeeping = query('purchase_get_order', "id = '$orderId'");
                    $children = find(array('table' => 'purchase_get_order_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'purchase_get_order_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money,ifnull(sum(moneyWithOutTax),0) as moneyWithOutTax,ifnull(sum(taxMoney),0) as taxMoney"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['moneyWithOutTax'];
                    $taxMoney = $total['taxMoney'];
                    $tableName = 'purchase_get_order';
                    break;
                case '采购退货':
                    $bookkeeping = query('purchase_refund_order', "id = '$orderId'");
                    $children = find(array('table' => 'purchase_refund_order_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'purchase_refund_order_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money,ifnull(sum(moneyWithOutTax),0) as moneyWithOutTax,ifnull(sum(taxMoney),0) as taxMoney"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['moneyWithOutTax'];
                    $taxMoney = $total['taxMoney'];
                    $tableName = 'purchase_refund_order';
                    break;
                case '销售出库':
                    $bookkeeping = query('sale_send_order', "id = '$orderId'");
                    $children = find(array('table' => 'sale_send_order_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'sale_send_order_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money,ifnull(sum(moneyWithOutTax),0) as moneyWithOutTax,ifnull(sum(taxMoney),0) as taxMoney"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['moneyWithOutTax'];
                    $taxMoney = $total['taxMoney'];
                    $tableName = 'sale_send_order';
                    break;
                case '销售退货':
                    $bookkeeping = query('sale_refund_order', "id = '$orderId'");
                    $children = find(array('table' => 'sale_refund_order_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'sale_refund_order_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money,ifnull(sum(moneyWithOutTax),0) as moneyWithOutTax,ifnull(sum(taxMoney),0) as taxMoney"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['moneyWithOutTax'];
                    $taxMoney = $total['taxMoney'];
                    $tableName = 'sale_refund_order';
                    break;
                case '采购暂估单':
                    $bookkeeping = query('purchase_esti_order', "id = '$orderId'");
                    $children = find(array('table' => 'purchase_esti_order_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'purchase_esti_order_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money,ifnull(sum(moneyWithOutTax),0) as moneyWithOutTax,ifnull(sum(taxMoney),0) as taxMoney"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['moneyWithOutTax'];
                    $taxMoney = $total['taxMoney'];
                    $tableName = 'purchase_esti_order';
                    break;
                case '领料单':
                case '退料单':
                case '生产入库单':
                case '出库':
                case '退货':
                case '盘盈':
                case '其他入库':
                case '盘亏':
                case '其他出库':
                    $bookkeeping = query('inventory_changes', "id = '$orderId'");
                    $children = find(array('table' => 'inventory_changes_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'inventory_changes_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money"));
                    $total['money'] = self::formattingFigure($total['money']);
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['money'];
                    $taxMoney = 0;
                    $tableName = 'inventory_changes';
                    $bookkeeping['recordDate'] = $bookkeeping['confirmTime']?date('Y-m-d',strtotime($bookkeeping['confirmTime'])):date('Y-m-d',strtotime($bookkeeping['time']));
                    break;
                case '销项发票':
                case '进项发票':
                    $bookkeeping = query('invoice_open', "id = '$orderId'");
                    if ($bookkeeping['type'] == '收票') {
                        $bookkeeping['supplierId'] = $bookkeeping['khid'];
                    }
                    $children = find(array('table' => 'invoice_open_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $orderMoney = $bookkeeping['money'];
                    $moneyWithNoTax = $bookkeeping['unTaxMoney'];
                    $taxMoney = $bookkeeping['taxMoney'];
                    $bookkeeping['orderNo'] = $bookkeeping['invoiceNo'];
                    $bookkeeping['recordDate'] = $bookkeeping['invoiceDate'];
                    $tableName = 'invoice_open';
                    break;
                case '收入单':
                case '费用单':
                    $bookkeeping = query('other_receipt', "id = '$orderId'");
                    if ($bookkeeping['target'] == '供应商') {
                        $bookkeeping['supplierId'] = $bookkeeping['targetId'];
                    } else {
                        //默认客户
                        $bookkeeping['khid'] = $bookkeeping['targetId'];
                    }
                    $children = find(array('table' => 'other_receipt_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'other_receipt_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['money'];
                    $taxMoney = 0;
                    $tableName = 'other_receipt';
                    break;
                case '收款单':
                case '付款单':
                    $bookkeeping = query('receipt', "id = '$orderId'");
                    if ($bookkeeping['target'] == '供应商') {
                        $bookkeeping['supplierId'] = $bookkeeping['targetId'];
                    } else {
                        //默认客户
                        $bookkeeping['khid'] = $bookkeeping['targetId'];
                    }
                    $children = find(array('table' => 'receipt_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'receipt_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['money'];
                    $taxMoney = 0;
                    $tableName = 'receipt';
                    break;
                case '成本调整单':
                    $bookkeeping = query('inventory_cost', "id = '$orderId'");
                    $children = find(array('table' => 'inventory_cost_detail', 'where' => "orderId = '$bookkeeping[id]' order by time asc"));
                    $total = find(array('table' => 'inventory_cost_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(price),0) as price"));
                    $orderMoney =$total['money'];
                    $moneyWithNoTax = $total['money'];
                    $taxMoney = 0;
                    $tableName = 'inventory_cost';
                    $bookkeeping['recordDate'] = $bookkeeping['orderDate'];
                    break;
                case '组装单':
                    $bookkeeping = query('inventory_assembly', "id = '$orderId'");
                    $children = find(array('table' => 'inventory_assembly_detail', 'where' => "orderId = '$bookkeeping[id]' order by time asc"));
                    $total = find(array('table' => 'inventory_assembly_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(price*num),0) as money"));
                    $orderMoney = 0;
                    $moneyWithNoTax = $total['money'];
                    $taxMoney = 0;
                    $tableName = 'inventory_assembly';
                    $bookkeeping['recordDate'] = $bookkeeping['orderDate'];
                    break;
                case '拆卸单':
                    $bookkeeping = query('inventory_dismantle', "id = '$orderId'");
                    $children = find(array('table' => 'inventory_dismantle_detail', 'where' => "orderId = '$bookkeeping[id]' order by time asc"));
                    $total = find(array('table' => 'inventory_dismantle_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(price*num),0) as money"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['money'];
                    $taxMoney = 0;
                    $tableName = 'inventory_dismantle';
                    $bookkeeping['recordDate'] = $bookkeeping['orderDate'];
                    break;
                case '转账单':
                    $bookkeeping = query('transfer_order', "id = '$orderId'");
                    $orderMoney = $bookkeeping['money'];
                    $moneyWithNoTax = $bookkeeping['money'];
                    $taxMoney = 0;
                    $tableName = 'transfer_order';
                    break;
                default:
                    if ($typeOne == '核销单') {
                        $bookkeeping = query('write_off_order', "id = '$orderId'");
                        $children = find(array('table' => 'write_off_order_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                        $total = find(array('table' => 'write_off_order_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money"));
                        $orderMoney = 0;
                        $moneyWithNoTax = $total['money'];
                        $taxMoney = 0;
                        $tableName = 'write_off_order';
                    } elseif ($typeOne == '费用报销单') {
                        $bookkeeping = query('cost', "id = '$orderId'");
                        $children = find(array('table' => 'cost_record', 'where' => "costId = '$bookkeeping[id]' order by time asc"));
                        $total = find(array('table' => 'cost_record', 'single' => "costId = '$orderId'", 'field' => "ifnull(sum(money),0) as money"));
                        $orderMoney = 0;
                        $moneyWithNoTax = $total['money'];
                        $taxMoney = 0;
                        $tableName = 'cost';
                    } else {
                        $bookkeeping = [
                            'orderNo' => '',
                            'recordDate' => '',
                        ];
                        $orderMoney = 0;//单据金额
                        $moneyWithNoTax = 0;//未税金额
                        $taxMoney = 0;//税额
                        $tableName = '';
                    }

                    break;
            }

            $json['warn'] = '';
            if (empty($bookkeeping['voucherId'])) {
                $template = query('business_voucher_template', "coid = '" . self::$coid . "' and type = '$typeTwo' and defaultTemplate = 1");
                $templateDetail = find(array('table' => 'business_voucher_template_detail', 'where' => "orderId = '$template[id]' order by list asc"));
                $voucherWord = query('voucher_word', "id = '$template[wordId]'");
                $voucherNo = voucherEditClass::voucherNo($template['wordId'], $bookkeeping['recordDate']);;
                $field = [
                    'recordDate' => $bookkeeping['recordDate'],
                    'voucherWordId' => $template['wordId'],
                    'voucherNo' => $voucherNo,
                ];
                $annotation = [
                    'recordDate' => '日期',
                    'voucherWordId' => '凭证字',
                    'voucherNo' => '凭证号',
                ];
                $rules = [
                    'recordDate' => 'required',
                    'voucherWordId' => 'required',
                    'voucherNo' => 'required|unsigned_int',
                ];
                // 校验是否符合规则
                $vt = new \verify();
                $result = $vt->setAliases($annotation)->validate($field, $rules)->isOk();
                if ($result === false) {
                    $text .= "单据编号【{$bookkeeping['orderNo']}】" . $vt->getErrs()[0] . "<br>";  // 多个规则提示最前面一条
                } elseif (checkoutClass::isChecked($field['recordDate'])) {
                    $text .= "单据编号【{$bookkeeping['orderNo']}】凭证字号【" . $voucherWord['word'] . '-' . $voucherNo . "】日期所处期间已结账<br>";
                } else {
                    $this->startTrans();

                    $field = array_merge($field, [
                        'id' => suiji(),
                        'coid' => self::$coid,
                        'isTemplate' => '否',
                        'stid' => self::$stid,
                        'source' => '正常',
                        'sourceId' => $orderId,
                        'sourceTable' => $tableName
                    ]);
                    $company = query('company', "coid = '" . self::$coid . "'");
                    if ($company['isAudit']) {
                        $field['workFlow'] = '待审核';
                    } else {
                        $field['workFlow'] = '已记账';
                    }
                    $json['warn'] = static::insert(["table" => 'voucher', "field" => $field]);
                    $money = 0;
                    $jieTotalMoney = $daiTotalMoney = 0;
                    foreach ($templateDetail['array'] as $array) {
                        $assitItemCode = $array['assitItem'] ? json_decode($array['assitItem'], true) : [];
                        $summaryField = $template['summary'] ? json_decode($template['summary'], true) : [];
                        $summary = $template['preSummary'] ? '_' . $template['preSummary'] : '';
                        if (isset($summaryField['orderNo']) && !empty($summaryField['orderNo'])) {
                            $summary .= '_' . $bookkeeping['orderNo'];
                        }
                        if (isset($summaryField['department']) && !empty($summaryField['department'])) {
                            $summary .= '_' . self::findStaffDepartment($bookkeeping['stid']);
                        }
                        if (isset($summaryField['staff']) && !empty($summaryField['staff'])) {
                            $summary .= '_' . self::findStaff($bookkeeping['stid']);
                        }
                        $summary .= $template['suffixSummary'] ? '_' . $template['suffixSummary'] : '';
                        $summary = ltrim($summary, '_');
                        if (in_array($array['rule'], ['明细价税合计', '明细金额', '明细税额'])) {
                            foreach ($children['array'] as $child) {
                                $child['materialId'] = $child['materialId'] ?? '';
                                $material = query('material', "id = '$child[materialId]'");
                                $subject = $this->subjectSource($array, $material['id']);
                                if (in_array($typeOne, ['费用报销单', '核销单'])) {
                                    //费用报销中存在不同类型对应不同科目的情况
                                    $business_voucher_subject = query('business_voucher_subject', "orderId = '$array[id]' and type = '$child[type]' and draft = 0 order by subjectCode asc");
                                    if (!empty($business_voucher_subject['id'])) {
                                        $subject = query('subject', "id = '$business_voucher_subject[subjectId]'");
                                    }
                                }
                                if (in_array($typeOne, $this->accountMainReceipt)) {
                                    //不同类型对应不同科目的情况
                                    $business_voucher_subject = query('business_voucher_subject', "orderId = '$array[id]' and type = '$child[project]' and draft = 0 order by subjectCode asc");
                                    if (!empty($business_voucher_subject['id'])) {
                                        $subject = query('subject', "id = '$business_voucher_subject[subjectId]'");
                                    }
                                }
                                if ($array['subjectSource'] == 'account' && in_array($typeOne, $this->accountReceipt)) {
                                    $account = query('account', "id = '$child[acid]'");
                                    $subject = query('subject', "id = '$account[subjectId]'");
                                }
                                $ruleMoney = match ($array['rule']) {
                                    '明细价税合计' => $child['money'] ?? 0,
                                    '明细金额' => $child['moneyWithOutTax'] ?? 0,
                                    '明细税额' => $child['taxMoney'] ?? 0,
                                    default => 0,
                                };
                                $ruleMoney = self::formattingFigure($ruleMoney);
                                if ($array['direction'] == '借') {
                                    $money += $ruleMoney;
                                    $jieMoney = $ruleMoney;
                                    $daiMoney = 0;
                                } else {
                                    $daiMoney = $ruleMoney;
                                    $jieMoney = 0;
                                }

                                $supplierId = $khid = $staffId = $departmentId = $projectId = $cashId = '';
                                if ($subject['assistAccount'] == '是') {
                                    $assitItemArr = explode(',', $subject['assistAccountIds']);
                                    foreach ($assitItemArr as $assitItem) {
                                        switch ($assitItem) {
                                            case '供应商':
                                                $supplierId = $bookkeeping['supplierId'];
                                                break;
                                            case '客户':
                                                $khid = $bookkeeping['khid'];
                                                break;
                                            case '员工':
                                                $staffId = $bookkeeping['stid'];
                                                break;
                                            case '部门':
                                                $staff = query('staff', "stid = '$bookkeeping[stid]'");
                                                $job = query('job', "id = '$staff[jobId]'");
                                                $departmentId = $job['departmentId'];
                                                break;
                                            case '项目':
                                                $project = query('project', "orderNo = '$assitItemCode[projectCode]' and coid = '" . self::$coid . "'");
                                                $projectId = $project['id'];
                                                break;
                                            case '现金流':
                                                $assitItemCode['cashCode'] = $assitItemCode['cashCode'] ?? '';
                                                $cash = query('cash', "code = '$assitItemCode[cashCode]' and coid = '" . self::$coid . "'");
                                                $cashId = $cash['id'];
                                                break;
                                        }
                                    }
                                }
                                $collection = [
                                    'voucherPrice' => 0,
                                    'voucherRate' => 0,
                                    'voucherMoney' => 0,
                                ];
                                if ($subject['numAccount'] == '是') {
                                    $collection = [
                                        'voucherPrice' => $child['price'],
                                        'voucherRate' => $child['rate'],
                                        'voucherMoney' => $child['money'],
                                    ];
                                }
//                                    $currency = query('currency',"isBase = '是' and coid = '".self::$coid."'");
                                $child['unit'] = $child['unit'] ?? $material['unit'];
                                $child['num'] = $child['num'] ?? 1;
                                $childField = [
                                    'id' => suiji(),
                                    'coid' => self::$coid,
                                    'orderId' => $field['id'],
                                    'summary' => $summary,
                                    'subjectId' => $subject['id'],
                                    'subjectName' => $subject['name'],
                                    'subjectCode' => $subject['code'],
                                    'direction' => $array['direction'],
                                    'money' => $jieMoney != 0 ? self::yuanToCent($jieMoney) : self::yuanToCent($daiMoney),
                                    'subjectAttr' => json_encode($collection, 320),
                                    'khid' => $khid,
                                    'supplierId' => $supplierId,
                                    'staffId' => $staffId,
                                    'projectId' => $projectId,
                                    'materialId' => $child['materialId'],
                                    'cashId' => $cashId,
                                    'departmentId' => $departmentId,
                                    'voucherNum' => $subject['numAccount'] == '是' ? machiningOrderClass::material_num_change($child['materialId'], $child['unit'], $material['unit'], $child['num']) : 0,
                                    'currencyId' => '',
                                    'unit' => $subject['numAccount'] == '是' ? $material['unit'] : '',
                                ];

                                $warn = static::insert(array(
                                    'table' => 'voucher_detail',
                                    'field' => $childField
                                ));
                                if (!in_array($warn, ["新增成功", "更新成功"])) {
                                    $json['warn'] = $warn;
                                    $text .= "单据编号【{$bookkeeping['orderNo']}】:" . $json['warn'] . "<br>";
                                    break;
                                }
                                if ($array['direction'] == '借') {
                                    $jieTotalMoney += self::centToYuan($childField['money']);
                                } else {
                                    $daiTotalMoney += self::centToYuan($childField['money']);
                                }
                            }

                        } else {
                            $subject = $this->subjectSource($array);
                            if ($array['subjectSource'] == 'account' && in_array($typeOne, $this->accountMainReceipt)) {
                                //账户在明细上的时候
//                                $account = query('account', "id = '$bookkeeping[acid]'");
                                $subject = query('subject', "id = '$bookkeeping[acid]'");
                            }
                            $supplierId = $khid = $staffId = $departmentId = $projectId = $cashId = '';
                            if ($subject['assistAccount'] == '是') {
                                $assitItemArr = explode(',', $subject['assistAccountIds']);
                                foreach ($assitItemArr as $assitItem) {
                                    switch ($assitItem) {
                                        case '供应商':
                                            $supplierId = $bookkeeping['supplierId'];
                                            break;
                                        case '客户':
                                            $khid = $bookkeeping['khid'];
                                            break;
                                        case '员工':
                                            $staffId = $bookkeeping['stid'];
                                            break;
                                        case '部门':
                                            $staff = query('staff', "stid = '$bookkeeping[stid]'");
                                            $job = query('job', "id = '$staff[jobId]'");
                                            $departmentId = $job['departmentId'];
                                            break;
                                        case '项目':
                                            $project = query('project', "orderNo = '$assitItemCode[projectCode]' and coid = '" . self::$coid . "'");
                                            $projectId = $project['id'];
                                            break;
                                        case '现金流':
                                            $assitItemCode['cashCode'] = $assitItemCode['cashCode'] ?? '';
                                            $cash = query('cash', "code = '$assitItemCode[cashCode]' and coid = '" . self::$coid . "'");
                                            $cashId = $cash['id'];
                                            break;
                                    }
                                }
                            }

                            $ruleMoney = match ($array['rule']) {
                                '汇总价税合计' => $orderMoney,
                                '汇总金额' => $moneyWithNoTax,
                                '汇总税额' => $taxMoney,
                                default => 0,
                            };
                            $ruleMoney = self::formattingFigure($ruleMoney);
                            if ($array['direction'] == '借') {
                                $money += $ruleMoney;
                                $jieMoney = $ruleMoney;
                                $daiMoney = 0;
                            } else {
                                $daiMoney = $ruleMoney;
                                $jieMoney = 0;
                            }
                            $collection = [
                                'voucherPrice' => 0,
                                'voucherRate' => 0,
                                'voucherMoney' => 0,
                            ];
                            $childField = [
                                'id' => suiji(),
                                'coid' => self::$coid,
                                'orderId' => $field['id'],
                                'summary' => $summary,
                                'subjectId' => $subject['id'],
                                'subjectName' => $subject['name'],
                                'subjectCode' => $subject['code'],
                                'direction' => $array['direction'],
                                'money' => $jieMoney != 0 ? self::yuanToCent($jieMoney) : self::yuanToCent($daiMoney),
                                'subjectAttr' => json_encode($collection, 320),
                                'khid' => $khid,
                                'supplierId' => $supplierId,
                                'staffId' => $staffId,
                                'projectId' => $projectId,
                                'materialId' => '',
                                'cashId' => $cashId,
                                'departmentId' => $departmentId,
                                'voucherNum' => 0,
                                'currencyId' => '',
                                'unit' => '',
                            ];
                            $warn = static::insert(array(
                                'table' => 'voucher_detail',
                                'field' => $childField
                            ));
                            if (!in_array($warn, ["新增成功", "更新成功"])) {
                                $json['warn'] = $warn;
                                $text .= "单据编号【{$bookkeeping['orderNo']}】:" . $json['warn'] . "<br>";
                                break;
                            }
                            if ($array['direction'] == '借') {
                                $jieTotalMoney += self::centToYuan($childField['money']);
                            } else {
                                $daiTotalMoney += self::centToYuan($childField['money']);
                            }
                        }
                        $warn = update(array('table' => 'voucher', 'field' => ['money' => $money], 'where' => "id = '$field[id]'"));
                        if ($warn != '更新成功') {
                            $json['warn'] = $warn;
                            $text .= "单据编号【{$bookkeeping['orderNo']}】:" . $json['warn'] . "<br>";
                            break;
                        }
                        $warn = update(array('table' => $tableName, 'field' => ['voucherId' => $field['id']], 'where' => "id = '$bookkeeping[id]'"));
                        if ($warn != '更新成功') {
                            $json['warn'] = $warn;
                            $text .= "单据编号【{$bookkeeping['orderNo']}】:" . $json['warn'] . "<br>";
                            break;
                        }
                    }
                    if (round($jieTotalMoney, 2) != round($daiTotalMoney, 2)) {
                        $this->rollback();
                        $text .= "单据编号【{$bookkeeping['orderNo']}】生成凭证失败:借贷不平衡<br>";
                    } elseif (!in_array($json['warn'], ['新增成功', '更新成功'])) {
                        $this->rollback();
                        $text .= "单据编号【{$bookkeeping['orderNo']}】生成凭证失败:编辑失败<br>";
                    } else {
                        $this->commitTrans();
                        $text .= "单据编号【{$bookkeeping['orderNo']}】生成凭证成功<br>";
                        $suc++;
                    }
                }

            }
        }
        $this->logText(["type" => self::page, "text" => $text]);
        $json['warn'] = 2;
        $json['href'] = root . "control/adIndex.html?cla=" . self::page . "&fun=home&id=".$typeTwo;
        $_SESSION['warn'] = "成功生成了{$suc}个凭证";
        return $json;
    }


    /**
     * 合并生成
     */
    public function editVoucherMerge($listId, $typeOne, $typeTwo)
    {
        $listArr = explode(',', $listId);
        $maxStrtotime = 0;
        $mergeData = [];
        $mergeSupplierData = [];//借
        $daiMergeSupplierData = [];//贷
        $orderMoneyTotal = $taxMoneyTotal = $moneyWithNoTaxTotal = 0;
        $mergeTable = [];
        foreach ($listArr as $orderId) {
            $children['array'] = [];
            switch ($typeTwo) {
//                case '领料单':
//                case '退料单':
//                    $bookkeeping = query('pick_order', "id = '$orderId'");
//                    $children = find(array('table' => 'pick_order_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
//                    $total = find(array('table' => 'pick_order_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money"));
//                    $orderMoney = self::formattingFigure($total['money']);
//                    $moneyWithNoTax = self::formattingFigure($total['money']);
//                    $taxMoney = 0;
//                    break;
                case '采购入库':
                    $bookkeeping = query('purchase_get_order', "id = '$orderId'");
                    $children = find(array('table' => 'purchase_get_order_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'purchase_get_order_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money,ifnull(sum(moneyWithOutTax),0) as moneyWithOutTax,ifnull(sum(taxMoney),0) as taxMoney"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['moneyWithOutTax'];
                    $taxMoney = $total['taxMoney'];
                    $mergeTable[$orderId] = 'purchase_get_order';
                    break;
                case '采购退货':
                    $bookkeeping = query('purchase_refund_order', "id = '$orderId'");
                    $children = find(array('table' => 'purchase_refund_order_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'purchase_refund_order_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money,ifnull(sum(moneyWithOutTax),0) as moneyWithOutTax,ifnull(sum(taxMoney),0) as taxMoney"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['moneyWithOutTax'];
                    $taxMoney = $total['taxMoney'];
                    $mergeTable[$orderId] = 'purchase_refund_order';
                    break;
                case '销售出库':
                    $bookkeeping = query('sale_send_order', "id = '$orderId'");
                    $children = find(array('table' => 'sale_send_order_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'sale_send_order_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money,ifnull(sum(moneyWithOutTax),0) as moneyWithOutTax,ifnull(sum(taxMoney),0) as taxMoney"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['moneyWithOutTax'];
                    $taxMoney = $total['taxMoney'];
                    $mergeTable[$orderId] = 'sale_send_order';
                    break;
                case '销售退货':
                    $bookkeeping = query('sale_refund_order', "id = '$orderId'");
                    $children = find(array('table' => 'sale_refund_order_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'sale_refund_order_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money,ifnull(sum(moneyWithOutTax),0) as moneyWithOutTax,ifnull(sum(taxMoney),0) as taxMoney"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['moneyWithOutTax'];
                    $taxMoney = $total['taxMoney'];
                    $mergeTable[$orderId] = 'sale_refund_order';
                    break;
                case '采购暂估单':
                    $bookkeeping = query('purchase_esti_order', "id = '$orderId'");
                    $children = find(array('table' => 'purchase_esti_order_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'purchase_esti_order_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money,ifnull(sum(moneyWithOutTax),0) as moneyWithOutTax,ifnull(sum(taxMoney),0) as taxMoney"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['moneyWithOutTax'];
                    $taxMoney = $total['taxMoney'];
                    $mergeTable[$orderId] = 'purchase_esti_order';
                    break;
                case '领料单':
                case '退料单':
                case '生产入库单':
                case '出库':
                case '退货':
                case '盘盈':
                case '其他入库':
                case '盘亏':
                case '其他出库':
                    $bookkeeping = query('inventory_changes', "id = '$orderId'");
                    $children = find(array('table' => 'inventory_changes_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'inventory_changes_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money"));
                    $total['money'] = self::formattingFigure($total['money']);
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['money'];
                    $taxMoney = 0;
                    $mergeTable[$orderId] = 'inventory_changes';
                    $bookkeeping['recordDate'] = $bookkeeping['confirmTime']?date('Y-m-d',strtotime($bookkeeping['confirmTime'])):date('Y-m-d',strtotime($bookkeeping['time']));
                    break;
                case '销项发票':
                case '进项发票':
                    $bookkeeping = query('invoice_open', "id = '$orderId'");
                    if ($bookkeeping['type'] == '收票') {
                        $bookkeeping['supplierId'] = $bookkeeping['khid'];
                    }
                    $children['array'] = [];
                    $orderMoney = $bookkeeping['money'];
                    $moneyWithNoTax = $bookkeeping['unTaxMoney'];
                    $taxMoney = $bookkeeping['taxMoney'];
                    $bookkeeping['orderNo'] = $bookkeeping['invoiceNo'];
                    $bookkeeping['recordDate'] = $bookkeeping['invoiceDate'];
                    $mergeTable[$orderId] = 'invoice_open';
                    break;
                case '收入单':
                case '费用单':
                    $bookkeeping = query('other_receipt', "id = '$orderId'");
                    if ($bookkeeping['target'] == '供应商') {
                        $bookkeeping['supplierId'] = $bookkeeping['targetId'];
                    } else {
                        //默认客户
                        $bookkeeping['khid'] = $bookkeeping['targetId'];
                    }
                    $children = find(array('table' => 'other_receipt_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'other_receipt_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['money'];
                    $taxMoney = 0;
                    $mergeTable[$orderId] = 'other_receipt';
                    break;
                case '收款单':
                case '付款单':
                    $bookkeeping = query('receipt', "id = '$orderId'");
                    if ($bookkeeping['target'] == '供应商') {
                        $bookkeeping['supplierId'] = $bookkeeping['targetId'];
                    } else {
                        //默认客户
                        $bookkeeping['khid'] = $bookkeeping['targetId'];
                    }
                    $children = find(array('table' => 'receipt_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'receipt_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['money'];
                    $taxMoney = 0;
                    $mergeTable[$orderId] = 'receipt';
                    break;
                case '成本调整单':
                    $bookkeeping = query('inventory_cost', "id = '$orderId'");
                    $children = find(array('table' => 'inventory_cost_detail', 'where' => "orderId = '$bookkeeping[id]' order by time asc"));
                    $total = find(array('table' => 'inventory_cost_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(price),0) as price"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['money'];
                    $taxMoney = 0;
                    $bookkeeping['recordDate'] = $bookkeeping['orderDate'];
                    $mergeTable[$orderId] = 'inventory_cost';
                    break;
                case '组装单':
                    $bookkeeping = query('inventory_assembly', "id = '$orderId'");
                    $children = find(array('table' => 'inventory_assembly_detail', 'where' => "orderId = '$bookkeeping[id]' order by time asc"));
                    $total = find(array('table' => 'inventory_assembly_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(price*num),0) as money"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['money'];
                    $taxMoney = 0;
                    $bookkeeping['recordDate'] = $bookkeeping['orderDate'];
                    $mergeTable[$orderId] = 'inventory_assembly';
                    break;
                case '拆卸单':
                    $bookkeeping = query('inventory_dismantle', "id = '$orderId'");
                    $children = find(array('table' => 'inventory_dismantle_detail', 'where' => "orderId = '$bookkeeping[id]' order by time asc"));
                    $total = find(array('table' => 'inventory_dismantle_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(price*num),0) as money"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['money'];
                    $taxMoney = 0;
                    $bookkeeping['recordDate'] = $bookkeeping['orderDate'];
                    $mergeTable[$orderId] = 'inventory_dismantle';
                    break;
                case '转账单':
                    $bookkeeping = query('transfer_order', "id = '$orderId'");
                    $orderMoney = $bookkeeping['money'];
                    $moneyWithNoTax = $bookkeeping['money'];
                    $taxMoney = 0;
                    $mergeTable[$orderId] = 'transfer_order';
                    break;
                default:
                    if ($typeOne == '核销单') {
                        $bookkeeping = query('write_off_order', "id = '$orderId'");
                        $children = find(array('table' => 'write_off_order_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                        $total = find(array('table' => 'write_off_order_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money"));
                        $orderMoney = $total['money'];
                        $moneyWithNoTax = $total['money'];
                        $taxMoney = 0;
                        $mergeTable[$orderId] = 'write_off_order';
                    } elseif ($typeOne == '费用报销单') {
                        $bookkeeping = query('cost', "id = '$orderId'");
                        $children = find(array('table' => 'cost_record', 'where' => "costId = '$bookkeeping[id]' order by time asc"));
                        $total = find(array('table' => 'cost_record', 'single' => "costId = '$orderId'", 'field' => "ifnull(sum(money),0) as money"));
                        $orderMoney = $total['money'];
                        $moneyWithNoTax = $total['money'];
                        $taxMoney = 0;
                        $mergeTable[$orderId] = 'cost';
                    } else {
                        $bookkeeping = [
                            'orderNo' => '',
                            'recordDate' => '',
                        ];
                        $orderMoney = 0;//单据金额
                        $moneyWithNoTax = 0;//未税金额
                        $taxMoney = 0;//税额
                    }

                    break;
            }
            $orderMoneyTotal += $orderMoney;
            $moneyWithNoTaxTotal += $moneyWithNoTax;
            $taxMoneyTotal += $taxMoney;
            $json['warn'] = '';
            if (empty($bookkeeping['voucherId'])) {
                if ($maxStrtotime < strtotime($bookkeeping['recordDate'])) {
                    $maxStrtotime = strtotime($bookkeeping['recordDate']);
                }
                $template = query('business_voucher_template', "coid = '" . self::$coid . "' and type = '$typeTwo' and defaultTemplate = 1");
                $voucherWord = query('voucher_word', "id = '$template[wordId]'");
                $templateDetail = find(array('table' => 'business_voucher_template_detail', 'where' => "orderId = '$template[id]' order by list asc"));

                foreach ($templateDetail['array'] as $array) {
                    if (in_array($array['rule'], ['明细价税合计', '明细金额', '明细税额'])) {
                        foreach ($children['array'] as $child) {
                            $child['money'] = $child['money'] ?? 0;
                            $child['taxMoney'] = $child['taxMoney'] ?? 0;
                            $child['moneyWithOutTax'] = $child['moneyWithOutTax'] ?? 0;
                            $mergeData = $this->accountMergArray($mergeData, $child, $typeOne);
                        }
                    } else {
                        $assitItemCode = $array['assitItem'] ? json_decode($array['assitItem'], true) : [];
                        $subject = $this->subjectSource($array);
                        if ($array['subjectSource'] == 'account' && in_array($typeOne, $this->accountMainReceipt)) {
                            //账户在主表上的时候
//                            $account = query('account', "id = '$bookkeeping[acid]'");
                            $subject = query('subject', "id = '$bookkeeping[acid]'");
                        }
                        if ($subject['assistAccount'] == '是') {
                            $assitItemArr = explode(',', $subject['assistAccountIds']);
                            if ($array['direction'] == '借') {
                                foreach ($assitItemArr as $assitItem) {
                                    switch ($assitItem) {
                                        case '供应商':
                                            if (!array_key_exists($bookkeeping['supplierId'], $mergeSupplierData)) {
                                                $mergeSupplierData[$bookkeeping['supplierId']] = [
                                                    'subjectId' => $subject['id'],
                                                    'orderMoney' => $orderMoney,
                                                    'taxMoney' => $taxMoney,
                                                    'moneyWithNoTax' => $moneyWithNoTax,
                                                ];
                                            } else {
                                                $mergeSupplierData[$bookkeeping['supplierId']] = [
                                                    'subjectId' => $subject['id'],
                                                    'orderMoney' => $orderMoney + $mergeSupplierData[$bookkeeping['supplierId']]['orderMoney'],
                                                    'taxMoney' => $taxMoney + $mergeSupplierData[$bookkeeping['supplierId']]['taxMoney'],
                                                    'moneyWithNoTax' => $moneyWithNoTax + $mergeSupplierData[$bookkeeping['supplierId']]['moneyWithNoTax'],
                                                ];
                                            }
                                            break;
                                        case '客户':
                                            if (!array_key_exists($bookkeeping['khid'], $mergeSupplierData)) {
                                                $mergeSupplierData[$bookkeeping['khid']] = [
                                                    'subjectId' => $subject['id'],
                                                    'orderMoney' => $orderMoney,
                                                    'taxMoney' => $taxMoney,
                                                    'moneyWithNoTax' => $moneyWithNoTax,
                                                ];
                                            } else {
                                                $mergeSupplierData[$bookkeeping['khid']] = [
                                                    'subjectId' => $subject['id'],
                                                    'orderMoney' => $orderMoney + $mergeSupplierData[$bookkeeping['khid']]['orderMoney'],
                                                    'taxMoney' => $taxMoney + $mergeSupplierData[$bookkeeping['khid']]['taxMoney'],
                                                    'moneyWithNoTax' => $moneyWithNoTax + $mergeSupplierData[$bookkeeping['khid']]['moneyWithNoTax'],
                                                ];
                                            }
                                            break;
                                        case '员工':
                                            $bookkeeping['stid'] = $bookkeeping['stid'] ?? '';
                                            if (!array_key_exists($bookkeeping['stid'], $mergeSupplierData)) {
                                                $mergeSupplierData[$bookkeeping['stid']] = [
                                                    'subjectId' => $subject['id'],
                                                    'orderMoney' => $orderMoney,
                                                    'taxMoney' => $taxMoney,
                                                    'moneyWithNoTax' => $moneyWithNoTax,
                                                ];
                                            } else {
                                                $mergeSupplierData[$bookkeeping['stid']] = [
                                                    'subjectId' => $subject['id'],
                                                    'orderMoney' => $orderMoney + $mergeSupplierData[$bookkeeping['stid']]['orderMoney'],
                                                    'taxMoney' => $taxMoney + $mergeSupplierData[$bookkeeping['stid']]['taxMoney'],
                                                    'moneyWithNoTax' => $moneyWithNoTax + $mergeSupplierData[$bookkeeping['stid']]['moneyWithNoTax'],
                                                ];
                                            }
                                            break;
                                        case '部门':
                                            $bookkeeping['stid'] = $bookkeeping['stid'] ?? '';
                                            $departmentName = self::findStaffDepartment($bookkeeping['stid']);
                                            if (!array_key_exists($departmentName, $mergeSupplierData)) {
                                                $mergeSupplierData[$departmentName] = [
                                                    'subjectId' => $subject['id'],
                                                    'orderMoney' => $orderMoney,
                                                    'taxMoney' => $taxMoney,
                                                    'moneyWithNoTax' => $moneyWithNoTax,
                                                ];
                                            } else {
                                                $mergeSupplierData[$departmentName] = [
                                                    'subjectId' => $subject['id'],
                                                    'orderMoney' => $orderMoney + $mergeSupplierData[$departmentName]['orderMoney'],
                                                    'taxMoney' => $taxMoney + $mergeSupplierData[$departmentName]['taxMoney'],
                                                    'moneyWithNoTax' => $moneyWithNoTax + $mergeSupplierData[$departmentName]['moneyWithNoTax'],
                                                ];
                                            }
                                            break;
                                        case '项目':
                                            $assitItemCode['projectCode'] = $assitItemCode['projectCode'] ?? '';
                                            if (!array_key_exists($assitItemCode['projectCode'], $mergeSupplierData)) {
                                                $mergeSupplierData[$assitItemCode['projectCode']] = [
                                                    'subjectId' => $subject['id'],
                                                    'orderMoney' => $orderMoney,
                                                    'taxMoney' => $taxMoney,
                                                    'moneyWithNoTax' => $moneyWithNoTax,
                                                ];
                                            } else {
                                                $mergeSupplierData[$assitItemCode['projectCode']] = [
                                                    'subjectId' => $subject['id'],
                                                    'orderMoney' => $orderMoney + $mergeSupplierData[$assitItemCode['projectCode']]['orderMoney'],
                                                    'taxMoney' => $taxMoney + $mergeSupplierData[$assitItemCode['projectCode']]['taxMoney'],
                                                    'moneyWithNoTax' => $moneyWithNoTax + $mergeSupplierData[$assitItemCode['projectCode']]['moneyWithNoTax'],
                                                ];
                                            }
                                            break;
                                        case '现金流':
                                            $assitItemCode['cashCode'] = $assitItemCode['cashCode'] ?? '';
                                            if (!array_key_exists($assitItemCode['cashCode'], $mergeSupplierData)) {
                                                $mergeSupplierData[$assitItemCode['cashCode']] = [
                                                    'subjectId' => $subject['id'],
                                                    'orderMoney' => $orderMoney,
                                                    'taxMoney' => $taxMoney,
                                                    'moneyWithNoTax' => $moneyWithNoTax,
                                                ];
                                            } else {
                                                $mergeSupplierData[$assitItemCode['cashCode']] = [
                                                    'subjectId' => $subject['id'],
                                                    'orderMoney' => $orderMoney + $mergeSupplierData[$assitItemCode['cashCode']]['orderMoney'],
                                                    'taxMoney' => $taxMoney + $mergeSupplierData[$assitItemCode['cashCode']]['taxMoney'],
                                                    'moneyWithNoTax' => $moneyWithNoTax + $mergeSupplierData[$assitItemCode['cashCode']]['moneyWithNoTax'],
                                                ];
                                            }
                                            break;
                                    }
                                }
                            } else {
                                foreach ($assitItemArr as $assitItem) {
                                    switch ($assitItem) {
                                        case '供应商':
                                            if (!array_key_exists($bookkeeping['supplierId'], $daiMergeSupplierData)) {
                                                $daiMergeSupplierData[$bookkeeping['supplierId']] = [
                                                    'orderMoney' => $orderMoney,
                                                    'taxMoney' => $taxMoney,
                                                    'moneyWithNoTax' => $moneyWithNoTax,
                                                    'subjectId' => $subject['id'],
                                                ];
                                            } else {
                                                $daiMergeSupplierData[$bookkeeping['supplierId']] = [
                                                    'orderMoney' => $orderMoney + $daiMergeSupplierData[$bookkeeping['supplierId']]['orderMoney'],
                                                    'taxMoney' => $taxMoney + $daiMergeSupplierData[$bookkeeping['supplierId']]['taxMoney'],
                                                    'moneyWithNoTax' => $moneyWithNoTax + $daiMergeSupplierData[$bookkeeping['supplierId']]['moneyWithNoTax'],
                                                    'subjectId' => $subject['id'],
                                                ];
                                            }
                                            break;
                                        case '客户':
                                            if (!array_key_exists($bookkeeping['khid'], $daiMergeSupplierData)) {
                                                $daiMergeSupplierData[$bookkeeping['khid']] = [
                                                    'orderMoney' => $orderMoney,
                                                    'taxMoney' => $taxMoney,
                                                    'moneyWithNoTax' => $moneyWithNoTax,
                                                    'subjectId' => $subject['id'],
                                                ];
                                            } else {
                                                $daiMergeSupplierData[$bookkeeping['khid']] = [
                                                    'orderMoney' => $orderMoney + $daiMergeSupplierData[$bookkeeping['khid']]['orderMoney'],
                                                    'taxMoney' => $taxMoney + $daiMergeSupplierData[$bookkeeping['khid']]['taxMoney'],
                                                    'moneyWithNoTax' => $moneyWithNoTax + $daiMergeSupplierData[$bookkeeping['khid']]['moneyWithNoTax'],
                                                    'subjectId' => $subject['id'],
                                                ];
                                            }
                                            break;
                                        case '员工':
                                            $bookkeeping['stid'] = $bookkeeping['stid'] ?? '';
                                            if (!array_key_exists($bookkeeping['stid'], $daiMergeSupplierData)) {
                                                $daiMergeSupplierData[$bookkeeping['stid']] = [
                                                    'orderMoney' => $orderMoney,
                                                    'taxMoney' => $taxMoney,
                                                    'moneyWithNoTax' => $moneyWithNoTax,
                                                    'subjectId' => $subject['id'],
                                                ];
                                            } else {
                                                $daiMergeSupplierData[$bookkeeping['stid']] = [
                                                    'orderMoney' => $orderMoney + $daiMergeSupplierData[$bookkeeping['stid']]['orderMoney'],
                                                    'taxMoney' => $taxMoney + $daiMergeSupplierData[$bookkeeping['stid']]['taxMoney'],
                                                    'moneyWithNoTax' => $moneyWithNoTax + $daiMergeSupplierData[$bookkeeping['stid']]['moneyWithNoTax'],
                                                    'subjectId' => $subject['id'],
                                                ];
                                            }
                                            break;
                                        case '部门':
                                            $bookkeeping['stid'] = $bookkeeping['stid'] ?? '';
                                            $departmentName = self::findStaffDepartment($bookkeeping['stid']);
                                            if (!array_key_exists($departmentName, $daiMergeSupplierData)) {
                                                $daiMergeSupplierData[$departmentName] = [
                                                    'orderMoney' => $orderMoney,
                                                    'taxMoney' => $taxMoney,
                                                    'moneyWithNoTax' => $moneyWithNoTax,
                                                    'subjectId' => $subject['id'],
                                                ];
                                            } else {
                                                $daiMergeSupplierData[$departmentName] = [
                                                    'orderMoney' => $orderMoney + $daiMergeSupplierData[$departmentName]['orderMoney'],
                                                    'taxMoney' => $taxMoney + $daiMergeSupplierData[$departmentName]['taxMoney'],
                                                    'moneyWithNoTax' => $moneyWithNoTax + $daiMergeSupplierData[$departmentName]['moneyWithNoTax'],
                                                    'subjectId' => $subject['id'],
                                                ];
                                            }
                                            break;
                                        case '项目':
                                            $assitItemCode['projectCode'] = $assitItemCode['projectCode'] ?? '';
                                            if (!array_key_exists($assitItemCode['projectCode'], $daiMergeSupplierData)) {
                                                $daiMergeSupplierData[$assitItemCode['projectCode']] = [
                                                    'orderMoney' => $orderMoney,
                                                    'taxMoney' => $taxMoney,
                                                    'moneyWithNoTax' => $moneyWithNoTax,
                                                    'subjectId' => $subject['id'],
                                                ];
                                            } else {
                                                $daiMergeSupplierData[$assitItemCode['projectCode']] = [
                                                    'orderMoney' => $orderMoney + $daiMergeSupplierData[$assitItemCode['projectCode']]['orderMoney'],
                                                    'taxMoney' => $taxMoney + $daiMergeSupplierData[$assitItemCode['projectCode']]['taxMoney'],
                                                    'moneyWithNoTax' => $moneyWithNoTax + $daiMergeSupplierData[$assitItemCode['projectCode']]['moneyWithNoTax'],
                                                    'subjectId' => $subject['id'],
                                                ];
                                            }
                                            break;
                                        case '现金流':
                                            $assitItemCode['cashCode'] = $assitItemCode['cashCode'] ?? '';
                                            if (!array_key_exists($assitItemCode['cashCode'], $daiMergeSupplierData)) {
                                                $daiMergeSupplierData[$assitItemCode['cashCode']] = [
                                                    'orderMoney' => $orderMoney,
                                                    'taxMoney' => $taxMoney,
                                                    'moneyWithNoTax' => $moneyWithNoTax,
                                                    'subjectId' => $subject['id'],
                                                ];
                                            } else {
                                                $daiMergeSupplierData[$assitItemCode['cashCode']] = [
                                                    'orderMoney' => $orderMoney + $daiMergeSupplierData[$assitItemCode['cashCode']]['orderMoney'],
                                                    'taxMoney' => $taxMoney + $daiMergeSupplierData[$assitItemCode['cashCode']]['taxMoney'],
                                                    'moneyWithNoTax' => $moneyWithNoTax + $daiMergeSupplierData[$assitItemCode['cashCode']]['moneyWithNoTax'],
                                                    'subjectId' => $subject['id'],
                                                ];
                                            }
                                            break;
                                        default:
                                            break;
                                    }
                                }
                            }

                        } else {
                            if ($array['direction'] == '借') {
                                if (!array_key_exists($subject['id'], $mergeSupplierData)) {
                                    $mergeSupplierData[$subject['id']] = [
                                        'subjectId' => $subject['id'],
                                        'orderMoney' => $orderMoney,
                                        'taxMoney' => $taxMoney,
                                        'moneyWithNoTax' => $moneyWithNoTax,
                                    ];
                                } else {
                                    $mergeSupplierData[$subject['id']] = [
                                        'subjectId' => $subject['id'],
                                        'orderMoney' => $orderMoney + $mergeSupplierData[$subject['id']]['orderMoney'],
                                        'taxMoney' => $taxMoney + $mergeSupplierData[$subject['id']]['taxMoney'],
                                        'moneyWithNoTax' => $moneyWithNoTax + $mergeSupplierData[$subject['id']]['moneyWithNoTax'],
                                    ];
                                }
                            } else {
                                if (!array_key_exists($subject['id'], $daiMergeSupplierData)) {
                                    $daiMergeSupplierData[$subject['id']] = [
                                        'subjectId' => $subject['id'],
                                        'orderMoney' => $orderMoney,
                                        'taxMoney' => $taxMoney,
                                        'moneyWithNoTax' => $moneyWithNoTax,
                                    ];
                                } else {
                                    $daiMergeSupplierData[$subject['id']] = [
                                        'subjectId' => $subject['id'],
                                        'orderMoney' => $orderMoney + $daiMergeSupplierData[$subject['id']]['orderMoney'],
                                        'taxMoney' => $taxMoney + $daiMergeSupplierData[$subject['id']]['taxMoney'],
                                        'moneyWithNoTax' => $moneyWithNoTax + $daiMergeSupplierData[$subject['id']]['moneyWithNoTax'],
                                    ];
                                }
                            }
                        }
                    }
                }
            }
        }

        $bookkeeping['orderNo'] = $bookkeeping['orderNo'] ?? '';
        $bookkeeping['stid'] = $bookkeeping['stid'] ?? '';
        $maxDays = date('t', $maxStrtotime);
        $recordDate = date('Y-m-' . sprintf('%02d', $maxDays), $maxStrtotime);
        $recordMonth = date('Y-m', $maxStrtotime);
        $voucherWord['id'] = $voucherWord['id'] ?? '';
        $voucherNo = voucherEditClass::voucherNo($voucherWord['id'], $recordDate);
        $field = [
            'recordDate' => $recordDate,
            'voucherWordId' => $voucherWord['id'],
            'voucherNo' => $voucherNo,
        ];
        $annotation = [
            'recordDate' => '日期',
            'voucherWordId' => '凭证字',
            'voucherNo' => '凭证号',
        ];
        $rules = [
            'recordDate' => 'required',
            'voucherWordId' => 'required',
            'voucherNo' => 'required|unsigned_int',
        ];
        // 校验是否符合规则
        $vt = new \verify();
        $result = $vt->setAliases($annotation)->validate($field, $rules)->isOk();
        if ($result === false) {
            $json['warn'] = $vt->getErrs()[0] . "<br>";  // 多个规则提示最前面一条
        } elseif (checkoutClass::isChecked($field['recordDate'])) {
            $json['warn'] = "日期所处期间已结账<br>";
        } else {
            $money = 0;
            $this->startTrans();
            $id = suiji();
            $templateDetail['array'] = $templateDetail['array'] ?? [];
            $template['preSummary'] = $template['preSummary'] ?? '';
            $template['suffixSummary'] = $template['suffixSummary'] ?? '';
            $field = array_merge($field, [
                'id' => $id,
                'coid' => self::$coid,
                'isTemplate' => '否',
                'stid' => self::$stid,
                'source' => '正常',
                'sourceId' => '',
                'sourceTable' => '',
            ]);
            $company = query('company', "coid = '" . self::$coid . "'");
            if ($company['isAudit']) {
                $field['workFlow'] = '待审核';
            } else {
                $field['workFlow'] = '已记账';
            }
            $json['warn'] = static::insert(["table" => 'voucher', "field" => $field]);
            $jieTotalMoney = $daiTotalMoney = 0;
            foreach ($templateDetail['array'] as $array) {
                $summary = $template['preSummary'] ? '_' . $template['preSummary'] : '';
                $summary .= '_' . $recordMonth . $typeOne . '合并';
                $summary .= $template['suffixSummary'] ? '_' . $template['suffixSummary'] : '';
                $summary = ltrim($summary, '_');
                if (in_array($array['rule'], ['明细价税合计', '明细金额', '明细税额'])) {
                    $subjectMerge = [];//合并科目
                    $subjectNotMerge = [];//不合并科目
                    foreach ($mergeData as $child) {
                        $child['materialId'] = $child['materialId'] ?? '';
                        $material = query('material', "id = '$child[materialId]'");
                        $subject = $this->subjectSource($array, $material['id']);
                        if (in_array($typeOne, ['费用报销单', '核销单'])) {
                            //费用报销中存在不同类型对应不同科目的情况
                            $business_voucher_subject = query('business_voucher_subject', "orderId = '$array[id]' and type = '$child[type]' and draft = 0 order by subjectCode asc");
                            if (!empty($business_voucher_subject['id'])) {
                                $subject = query('subject', "id = '$business_voucher_subject[subjectId]'");
                            }
                        }
                        if (in_array($typeOne, $this->accountMainReceipt)) {
                            //不同类型对应不同科目的情况
                            $business_voucher_subject = query('business_voucher_subject', "orderId = '$array[id]' and type = '$child[project]' and draft = 0 order by subjectCode asc");
                            if (!empty($business_voucher_subject['id'])) {
                                $subject = query('subject', "id = '$business_voucher_subject[subjectId]'");
                            }
                        }
                        if ($array['subjectSource'] == 'account' && in_array($typeOne, $this->accountReceipt)) {
                            $account = query('account', "id = '$child[acid]'");
                            $subject = query('subject', "id = '$account[subjectId]'");
                        }
                        $assitItemArr = [];
                        if ($subject['assistAccount'] == '是') {
                            $assitItemArr = explode(',', $subject['assistAccountIds']);
                        }

                        $ruleMoney = match ($array['rule']) {
                            '明细价税合计' => $child['money'],
                            '明细金额' => $child['moneyWithOutTax'],
                            '明细税额' => $child['taxMoney'],
                            default => 0,
                        };
                        $ruleMoney = self::formattingFigure($ruleMoney);
                        if ($array['direction'] == '借') {
                            $money += $ruleMoney;
                            $jieMoney = $ruleMoney;
                            $daiMoney = 0;
                        } else {
                            $daiMoney = $ruleMoney;
                            $jieMoney = 0;
                        }

                        if (in_array('存货', $assitItemArr)) {
                            $subjectNotMerge[] = [
                                'summary' => $summary,
                                'subjectId' => $subject['id'],
                                'materialId' => $child['materialId'],
                                'jieMoney' => $jieMoney,
                                'daiMoney' => $daiMoney,
                            ];
                        } else {
                            $jMoney = $subjectMerge[$subject['id']]['jieMoney'] ?? 0;
                            $dMoney = $subjectMerge[$subject['id']]['daiMoney'] ?? 0;
                            $subjectMerge[$subject['id']] = [
                                'summary' => $summary,
                                'jieMoney' => $jieMoney + $jMoney,
                                'daiMoney' => $daiMoney + $dMoney
                            ];
                        }
                    }
                    foreach ($subjectNotMerge as $child) {
                        $material = query('material', "id = '$child[materialId]'");
                        $subject = query('subject', "id = '$child[subjectId]'");
                        $collection = [
                            'voucherPrice' => 0,
                            'voucherRate' => 0,
                            'voucherMoney' => 0,
                        ];
                        if ($subject['numAccount'] == '是') {
                            $collection = [
                                'voucherPrice' => $child['price'],
                                'voucherRate' => $child['rate'],
                                'voucherMoney' => $child['money'],
                            ];
                        }
                        $child['unit'] = $child['unit'] ?? $material['unit'];
                        $child['num'] = $child['num'] ?? 1;
                        $childField = [
                            'id' => suiji(),
                            'coid' => self::$coid,
                            'orderId' => $field['id'],
                            'summary' => $child['summary'],
                            'subjectId' => $subject['id'],
                            'subjectName' => $subject['name'],
                            'subjectCode' => $subject['code'],
                            'direction' => $array['direction'],
                            'money' => $child['jieMoney'] != 0 ? self::yuanToCent($child['jieMoney']) : self::yuanToCent($child['daiMoney']),
                            'subjectAttr' => json_encode($collection, 320),
                            'khid' => '',
                            'supplierId' => '',
                            'staffId' => '',
                            'projectId' => '',
                            'materialId' => $child['materialId'],
                            'cashId' => '',
                            'departmentId' => '',
                            'voucherNum' => $subject['numAccount'] == '是' ? machiningOrderClass::material_num_change($child['materialId'], $child['unit'], $material['unit'], $child['num']) : 0,
                            'currencyId' => '',
                            'unit' => $subject['numAccount'] == '是' ? $material['unit'] : '',
                        ];
                        $warn = static::insert(array(
                            'table' => 'voucher_detail',
                            'field' => $childField
                        ));
                        if (!in_array($warn, ["新增成功", "更新成功"])) {
                            $json['warn'] = $warn;
                            break;
                        }
                        if ($array['direction'] == '借') {
                            $jieTotalMoney += self::centToYuan($childField['money']);
                        } else {
                            $daiTotalMoney += self::centToYuan($childField['money']);
                        }
                    }
                    foreach ($subjectMerge as $subjectId => $child) {
                        $subject = query('subject', "id = '$subjectId'");
                        $collection = [
                            'voucherPrice' => 0,
                            'voucherRate' => 0,
                            'voucherMoney' => 0,
                        ];
                        if ($subject['numAccount'] == '是') {
                            $collection = [
                                'voucherPrice' => $child['price'],
                                'voucherRate' => $child['rate'],
                                'voucherMoney' => $child['money'],
                            ];
                        }
                        $childField = [
                            'id' => suiji(),
                            'coid' => self::$coid,
                            'orderId' => $field['id'],
                            'summary' => $child['summary'],
                            'subjectId' => $subject['id'],
                            'subjectName' => $subject['name'],
                            'subjectCode' => $subject['code'],
                            'direction' => $array['direction'],
                            'money' => $child['jieMoney'] != 0 ? self::yuanToCent($child['jieMoney']) : self::yuanToCent($child['daiMoney']),
                            'subjectAttr' => json_encode($collection, 320),
                            'khid' => '',
                            'supplierId' => '',
                            'staffId' => '',
                            'projectId' => '',
                            'materialId' => '',
                            'cashId' => '',
                            'departmentId' => '',
                            'voucherNum' => 0,
                            'currencyId' => '',
                            'unit' => '',
                        ];
                        $warn = static::insert(array(
                            'table' => 'voucher_detail',
                            'field' => $childField
                        ));
                        if (!in_array($warn, ["新增成功", "更新成功"])) {
                            $json['warn'] = $warn;
                            break;
                        }
                        if ($array['direction'] == '借') {
                            $jieTotalMoney += self::centToYuan($childField['money']);
                        } else {
                            $daiTotalMoney += self::centToYuan($childField['money']);
                        }
                    }
                } elseif (in_array($array['rule'], ['汇总价税合计', '汇总金额', '汇总税额'])) {
                    $subjectRule = $this->subjectSource($array);
                    if ($array['subjectSource'] == 'account' && in_array($typeOne, $this->accountMainReceipt)) {
                        //账户在主表上的时候
                        $subjectRule = query('subject',"id = '$bookkeeping[acid]'");
                    }
                    if ($array['direction'] == '贷') {
                        $loopArr = $daiMergeSupplierData;
                    } else {
                        $loopArr = $mergeSupplierData;
                    }
                    foreach ($loopArr as $targetId => $value) {
                        if ($subjectRule['id'] != $value['subjectId']){
                            continue;
                        }
                        $subject = query('subject', "id = '$value[subjectId]'");
                        $supplierId = $khid = $staffId = $departmentId = $projectId = $cashId = '';
                        if ($subject['assistAccount'] == '是') {
                            $assitItemArr = explode(',', $subject['assistAccountIds']);
                            foreach ($assitItemArr as $assitItem) {
                                switch ($assitItem) {
                                    case '供应商':
                                        $supplierId = $targetId;
                                        break;
                                    case '客户':
                                        $khid = $targetId;
                                        break;
                                    case '员工':
                                        $staffId = $targetId;
                                        break;
                                    case '部门':
                                        $staff = query('staff', "stid = '$targetId'");
                                        $job = query('job', "id = '$staff[jobId]'");
                                        $departmentId = $job['departmentId'];
                                        break;
                                    case '项目':
                                        $project = query('project', "orderNo = '$targetId' and coid = '" . self::$coid . "'");
                                        $projectId = $project['id'];
                                        break;
                                    case '现金流':
                                        $cash = query('cash', "code = '$targetId' and coid = '" . self::$coid . "'");
                                        $cashId = $cash['id'];
                                        break;
                                }
                            }
                        } else {
                            //没有辅助核算
                            if (!$targetId) {
                                continue;
                            }
                            $subject = query('subject', "id = '$targetId'");
                        }
                        $ruleMoney = match ($array['rule']) {
                            '汇总价税合计' => $value['orderMoney'],
                            '汇总金额' => $value['moneyWithNoTax'],
                            '汇总税额' => $value['taxMoney'],
                            default => 0,
                        };
                        if ($array['direction'] == '借') {
                            $money += $ruleMoney;
                            $jieMoney = $ruleMoney;
                            $daiMoney = 0;
                        } else {
                            $daiMoney = $ruleMoney;
                            $jieMoney = 0;
                        }
                        $collection = [
                            'voucherPrice' => 0,
                            'voucherRate' => 0,
                            'voucherMoney' => 0,
                        ];
                        $childField = [
                            'id' => suiji(),
                            'coid' => self::$coid,
                            'orderId' => $id,
                            'summary' => $summary,
                            'subjectId' => $subject['id'],
                            'subjectName' => $subject['name'],
                            'subjectCode' => $subject['code'],
                            'direction' => $array['direction'],
                            'money' => $jieMoney != 0 ? self::yuanToCent($jieMoney) : self::yuanToCent($daiMoney),
                            'subjectAttr' => json_encode($collection, 320),
                            'khid' => $khid,
                            'supplierId' => $supplierId,
                            'staffId' => $staffId,
                            'projectId' => $projectId,
                            'materialId' => '',
                            'cashId' => $cashId,
                            'departmentId' => $departmentId,
                            'voucherNum' => 0,
                            'currencyId' => '',
                            'unit' => '',
                        ];


                        $warn = static::insert(array(
                            'table' => 'voucher_detail',
                            'field' => $childField
                        ));
                        if (!in_array($warn, ["新增成功", "更新成功"])) {
                            $json['warn'] = $warn;
                            break;
                        }
                        if ($array['direction'] == '借') {
                            $jieTotalMoney += self::centToYuan($childField['money']);
                        } else {
                            $daiTotalMoney += self::centToYuan($childField['money']);
                        }
                    }

                } else {
                    $json['warn'] = '此类型单据不在可合并范围内';
                }
            }
            $warn = update(array('table' => 'voucher', 'field' => ['money' => $money], 'where' => "id = '$field[id]'"));
            if ($warn != '更新成功') {
                $json['warn'] = $warn;
            }
            foreach ($mergeTable as $keyId => $tableName) {
                self::update(['table' => $tableName, 'field' => ['voucherId' => $field['id']], 'where' => "id = '$keyId'"]);
            }
            if (round($jieTotalMoney, 2) != round($daiTotalMoney, 2)) {
                $json['warn'] = '借贷不平衡';
                $this->rollback();
            } elseif (!in_array($json['warn'], ['新增成功', '更新成功'])) {
                $this->rollback();
            } else {
                $this->commitTrans();
                $text = $this->control['name'] . "生成了凭证【{$voucherWord['word']}-{$field['voucherNo']}】";
                $this->logText(["type" => self::page, "text" => $text]);
                $json['warn'] = 2;
                $_SESSION['warn'] = "生成凭证成功";
            }
        }
        $json['href'] = root . "control/adIndex.html?cla=" . self::page . "&fun=home";
        return $json;
    }

    /**
     * 生成凭证页面
     */
    public function generateVoucher(): string
    {
        $listId = $this->get('listId');
        $listArr = explode(',', $listId);
        $typeOne = $this->get('typeOne');//一级类别
        $typeTwo = $this->get('typeTwo');//二级类别
        if (empty($typeOne) || empty($typeTwo)) {
            $data['html'] = '未找到类型';
        } else {
            $listArr = array_reverse($listArr);
            $html = $this->previewVoucher($listArr, $typeOne, $typeTwo);
            $button = '';
            if (in_array($typeOne, [
                '采购单',
                '销售单',
                '采购暂估单',
                '领料单',
                '退料单',
                '生产入库单',
                '其他入库单',
                '其他出库单',
                '成本调整单',
                '结转出库成本',
                '收款单',
                '付款单',
                '费用单',
                '收入单',
                '销项发票',
                '进项发票',
            ])) {
                $button = <<<eof
<span style="margin-left: 20px"><label class='checkbox'><input name='mergeVoucher' onclick="mergeVouchers(this)" type='checkbox'  value='1'><span></span>&nbsp;合并生成凭证</label></span>
eof. componentClass::tipComponent('单个辅助项才能进项合并');

            }
            $data['html'] = <<<efo

        <form name="voucherForm">
        <div class="voucher-body">
            <div class="calendarBody">
                <div class="voucher-button">
                    <span class="spanButton li-btn-blue-bg" style="margin-left: 0" onclick="subForm('voucherForm',root+'control/adIndex.html?cla={$this->get('cla')}&fun=voucherEdit')">保存</span>
                    $button
                </div>
                <div id="inputTitle" class="input-div bold">
                    <div>业务单据</div>
                    <div>凭证预览</div>
                </div>
               $html
            </div>
        </div>
        <input name="listId" type="hidden" value="$listId">
        <input name="typeOne" type="hidden" value="$typeOne">
        <input name="typeTwo" type="hidden" value="$typeTwo">
        </form>
<script>
function mergeVouchers(e){
    let type = 0;
   if ($(e).is(":checked")){
       type = 1;
   }

   $.post(root+'control/adIndex.html?cla=businessVoucher&fun=voucherPage',{type:type,listId:$("[name='listId']").val(),typeOne:$("[name='typeOne']").val(),typeTwo:$("[name='typeTwo']").val()},function(res) {
       $(".content-box").remove();
       $("#inputTitle").after(res.html);
   },'json')
}
</script>
efo;
            $data['html'] .= $this->voucherCass();
        }


        //返回
        return $this->tab($data);
    }

    public function voucherPage()
    {
        $listId = $this->post('listId');
        $listArr = explode(',', $listId);
        $typeOne = $this->post('typeOne');//一级类别
        $typeTwo = $this->post('typeTwo');//二级类别
        $json['html'] = match ($this->post('type')) {
            '1' => $this->previewVoucherMerge($listArr, $typeOne, $typeTwo),
            default => $this->previewVoucher($listArr, $typeOne, $typeTwo),
        };
        return json_encode($json, 320);
    }

    /**
     *合并凭证
     */
    public function previewVoucherMerge($listArr, $typeOne, $typeTwo)
    {
        $lefItem = '';
        $orderMoney = 0;//单据金额
        $moneyWithNoTax = 0;//未税金额
        $taxMoney = 0;//税额
        $maxStrtotime = 0;
        $mergeData = [];
        $mergeSupplierData = [];//借
        $daiMergeSupplierData = [];//贷
        $orderMoneyTotal = $taxMoneyTotal = $moneyWithNoTaxTotal = 0;
        foreach ($listArr as $orderId) {
            switch ($typeTwo) {
                case '采购入库':
                    $bookkeeping = query('purchase_get_order', "id = '$orderId'");
                    $children = find(array('table' => 'purchase_get_order_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'purchase_get_order_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money,ifnull(sum(moneyWithOutTax),0) as moneyWithOutTax,ifnull(sum(taxMoney),0) as taxMoney"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['moneyWithOutTax'];
                    $taxMoney = $total['taxMoney'];
                    break;
                case '采购退货':
                    $bookkeeping = query('purchase_refund_order', "id = '$orderId'");
                    $children = find(array('table' => 'purchase_refund_order_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'purchase_refund_order_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money,ifnull(sum(moneyWithOutTax),0) as moneyWithOutTax,ifnull(sum(taxMoney),0) as taxMoney"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['moneyWithOutTax'];
                    $taxMoney = $total['taxMoney'];
                    break;
                case '销售出库':
                    $bookkeeping = query('sale_send_order', "id = '$orderId'");
                    $children = find(array('table' => 'sale_send_order_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'sale_send_order_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money,ifnull(sum(moneyWithOutTax),0) as moneyWithOutTax,ifnull(sum(taxMoney),0) as taxMoney"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['moneyWithOutTax'];
                    $taxMoney = $total['taxMoney'];
                    break;
                case '销售退货':
                    $bookkeeping = query('sale_refund_order', "id = '$orderId'");
                    $children = find(array('table' => 'sale_refund_order_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'sale_refund_order_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money,ifnull(sum(moneyWithOutTax),0) as moneyWithOutTax,ifnull(sum(taxMoney),0) as taxMoney"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['moneyWithOutTax'];
                    $taxMoney = $total['taxMoney'];
                    break;
                case '采购暂估单':
                    $bookkeeping = query('purchase_esti_order', "id = '$orderId'");
                    $children = find(array('table' => 'purchase_esti_order_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'purchase_esti_order_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money,ifnull(sum(moneyWithOutTax),0) as moneyWithOutTax,ifnull(sum(taxMoney),0) as taxMoney"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['moneyWithOutTax'];
                    $taxMoney = $total['taxMoney'];
                    break;
                case '收入单':
                case '费用单':
                    $bookkeeping = query('other_receipt', "id = '$orderId'");
                    if ($bookkeeping['target'] == '供应商') {
                        $bookkeeping['supplierId'] = $bookkeeping['targetId'];
                    } else {
                        //默认客户
                        $bookkeeping['khid'] = $bookkeeping['targetId'];
                    }
                    $children = find(array('table' => 'other_receipt_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'other_receipt_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['money'];
                    $taxMoney = 0;
                    break;
                case '收款单':
                case '付款单':
                    $bookkeeping = query('receipt', "id = '$orderId'");
                    if ($bookkeeping['target'] == '供应商') {
                        $bookkeeping['supplierId'] = $bookkeeping['targetId'];
                    } else {
                        //默认客户
                        $bookkeeping['khid'] = $bookkeeping['targetId'];
                    }
                    $children = find(array('table' => 'receipt_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'receipt_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['money'];
                    $taxMoney = 0;
                    break;
//                case '领料单':
//                case '退料单':
//                    $bookkeeping = query('pick_order', "id = '$orderId'");
//                    $children = find(array('table' => 'pick_order_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
//                    $total = find(array('table' => 'pick_order_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money"));
//                    $orderMoney = self::formattingFigure($total['money']);
//                    $moneyWithNoTax = self::formattingFigure($total['money']);
//                    $taxMoney = 0;
//                    break;
                case '销项发票':
                case '进项发票':
                    $bookkeeping = query('invoice_open', "id = '$orderId'");
                    if ($bookkeeping['type'] == '收票') {
                        $bookkeeping['supplierId'] = $bookkeeping['khid'];
                    }
                    $children['array'] = [];
                    $orderMoney = $bookkeeping['money'];
                    $moneyWithNoTax = $bookkeeping['unTaxMoney'];
                    $taxMoney = $bookkeeping['taxMoney'];
                    $bookkeeping['orderNo'] = $bookkeeping['invoiceNo'];
                    $bookkeeping['recordDate'] = $bookkeeping['invoiceDate'];
                    break;
                case '领料单':
                case '退料单':
                case '生产入库单':
                case '出库':
                case '退货':
                case '盘盈':
                case '其他入库':
                case '盘亏':
                case '其他出库':
                    $bookkeeping = query('inventory_changes', "id = '$orderId'");
                    $children = find(array('table' => 'inventory_changes_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'inventory_changes_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money"));
                    $total['money'] = self::formattingFigure($total['money']);
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['money'];
                    $taxMoney = 0;
                    $bookkeeping['recordDate'] = $bookkeeping['confirmTime']?date('Y-m-d',strtotime($bookkeeping['confirmTime'])):date('Y-m-d',strtotime($bookkeeping['time']));
                    break;
                case '成本调整单':
                    $bookkeeping = query('inventory_cost', "id = '$orderId'");
                    $children = find(array('table' => 'inventory_cost_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'inventory_cost_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(orderPrice),0) as money"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['money'];
                    $taxMoney = 0;
                    $bookkeeping['recordDate'] = $bookkeeping['orderDate'];
                    break;
                case '组装单':
                    $bookkeeping = query('inventory_assembly', "id = '$orderId'");
                    $children = find(array('table' => 'inventory_assembly_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'inventory_assembly_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(price*num),0) as money"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['money'];
                    $taxMoney = 0;
                    $bookkeeping['recordDate'] = $bookkeeping['orderDate'];
                    break;
                case '拆卸单':
                    $bookkeeping = query('inventory_dismantle', "id = '$orderId'");
                    $children = find(array('table' => 'inventory_dismantle_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'inventory_dismantle_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(price*num),0) as money"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['money'];
                    $taxMoney = 0;
                    $bookkeeping['recordDate'] = $bookkeeping['orderDate'];
                    break;
                case '转账单':
                    $bookkeeping = query('transfer_order', "id = '$orderId'");
                    $children['array'] = [];
                    $total = find(array('table' => 'transfer_order', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['money'];
                    $taxMoney = 0;
                    break;
                default:
                    if ($typeOne == '费用报销单') {
                        $bookkeeping = query('cost', "id = '$orderId'");
                        $children = find(array('table' => 'cost_record', 'where' => "costId = '$bookkeeping[id]' order by time asc"));
                        $total = find(array('table' => 'cost_record', 'single' => "costId = '$orderId'", 'field' => "ifnull(sum(money),0) as money"));
                        $orderMoney = $total['money'];
                        $moneyWithNoTax = $total['money'];
                        $taxMoney = 0;
                    } elseif ($typeOne == '核销单') {
                        $bookkeeping = query('write_off_order_detail', "id = '$orderId'");
                        $children = find(array('table' => 'write_off_order_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                        $total = find(array('table' => 'write_off_order_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money"));
                        $orderMoney = $total['money'];
                        $moneyWithNoTax = $total['money'];
                        $taxMoney = 0;
                    } else {
                        $bookkeeping = [
                            'orderNo' => '',
                            'recordDate' => '',
                        ];
                        $children['array'] = [];
                    }
                    break;
            }
            $orderMoneyTotal += $orderMoney;
            $moneyWithNoTaxTotal += $moneyWithNoTax;
            $taxMoneyTotal += $taxMoney;
            if (empty($bookkeeping['voucherId'])) {
                if ($maxStrtotime < strtotime($bookkeeping['recordDate'])) {
                    $maxStrtotime = strtotime($bookkeeping['recordDate']);
                }
                $template = query('business_voucher_template', "coid = '" . self::$coid . "' and type = '$typeTwo' and defaultTemplate = 1");
                $templateDetail = find(array('table' => 'business_voucher_template_detail', 'where' => "orderId = '$template[id]' order by list asc"));
                $voucherWord = query('voucher_word', "id = '$template[wordId]'");
                $lefItem .= <<<eof
                        <div class="left-box-item">
                            <div class="item-li-no">单据编号：$bookkeeping[orderNo]</div>
                            <div class="item-li-content">
                                <div>日期：{$bookkeeping['recordDate']}</div>
                                <div>业务类型：{$typeTwo}</div>
                                <div>单据金额：{$orderMoney}</div>
                            </div>
                        </div>
eof;
                foreach ($templateDetail['array'] as $array) {
                    if (in_array($array['rule'], ['明细价税合计', '明细金额', '明细税额'])) {
                        foreach ($children['array'] as $child) {
                            $child['money'] = $child['money'] ?? 0;
                            $child['taxMoney'] = $child['taxMoney'] ?? 0;
                            $child['moneyWithOutTax'] = $child['moneyWithOutTax'] ?? 0;
                            $child['materialId'] = $child['materialId'] ?? '';
                            $mergeData = $this->accountMergArray($mergeData, $child, $typeOne);
                        }
                    } else {
                        $assitItemCode = $array['assitItem'] ? json_decode($array['assitItem'], true) : [];
                        $subject = $this->subjectSource($array);
                        if ($array['subjectSource'] == 'account' && in_array($typeOne, $this->accountMainReceipt)) {
                            //账户在主表上的时候
//                            $subject = $this->subjectSource($array, $bookkeeping['acid']);
                            $subject = query('subject',"id = '$bookkeeping[acid]'");
                        }
                        if ($subject['assistAccount'] == '是') {
                            $assitItemArr = explode(',', $subject['assistAccountIds']);
                            if ($array['direction'] == '借') {
                                foreach ($assitItemArr as $assitItem) {
                                    switch ($assitItem) {
                                        case '供应商':
                                            if (!array_key_exists($bookkeeping['supplierId'], $mergeSupplierData)) {
                                                $mergeSupplierData[$bookkeeping['supplierId']] = [
                                                    'subjectId' => $subject['id'],
                                                    'orderMoney' => $orderMoney,
                                                    'taxMoney' => $taxMoney,
                                                    'moneyWithNoTax' => $moneyWithNoTax,
                                                ];
                                            } else {
                                                $mergeSupplierData[$bookkeeping['supplierId']] = [
                                                    'subjectId' => $subject['id'],
                                                    'orderMoney' => $orderMoney + $mergeSupplierData[$bookkeeping['supplierId']]['orderMoney'],
                                                    'taxMoney' => $taxMoney + $mergeSupplierData[$bookkeeping['supplierId']]['taxMoney'],
                                                    'moneyWithNoTax' => $moneyWithNoTax + $mergeSupplierData[$bookkeeping['supplierId']]['moneyWithNoTax'],
                                                ];
                                            }
                                            break;
                                        case '客户':
                                            if (!array_key_exists($bookkeeping['khid'], $mergeSupplierData)) {
                                                $mergeSupplierData[$bookkeeping['khid']] = [
                                                    'subjectId' => $subject['id'],
                                                    'orderMoney' => $orderMoney,
                                                    'taxMoney' => $taxMoney,
                                                    'moneyWithNoTax' => $moneyWithNoTax,
                                                ];
                                            } else {
                                                $mergeSupplierData[$bookkeeping['khid']] = [
                                                    'subjectId' => $subject['id'],
                                                    'orderMoney' => $orderMoney + $mergeSupplierData[$bookkeeping['khid']]['orderMoney'],
                                                    'taxMoney' => $taxMoney + $mergeSupplierData[$bookkeeping['khid']]['taxMoney'],
                                                    'moneyWithNoTax' => $moneyWithNoTax + $mergeSupplierData[$bookkeeping['khid']]['moneyWithNoTax'],
                                                ];
                                            }
                                            break;
                                        case '员工':
                                            $bookkeeping['stid'] = $bookkeeping['stid'] ?? '';
                                            if (!array_key_exists($bookkeeping['stid'], $mergeSupplierData)) {
                                                $mergeSupplierData[$bookkeeping['stid']] = [
                                                    'subjectId' => $subject['id'],
                                                    'orderMoney' => $orderMoney,
                                                    'taxMoney' => $taxMoney,
                                                    'moneyWithNoTax' => $moneyWithNoTax,
                                                ];
                                            } else {
                                                $mergeSupplierData[$bookkeeping['stid']] = [
                                                    'subjectId' => $subject['id'],
                                                    'orderMoney' => $orderMoney + $mergeSupplierData[$bookkeeping['stid']]['orderMoney'],
                                                    'taxMoney' => $taxMoney + $mergeSupplierData[$bookkeeping['stid']]['taxMoney'],
                                                    'moneyWithNoTax' => $moneyWithNoTax + $mergeSupplierData[$bookkeeping['stid']]['moneyWithNoTax'],
                                                ];
                                            }
                                            break;
                                        case '部门':
                                            $bookkeeping['stid'] = $bookkeeping['stid'] ?? '';
                                            $departmentName = self::findStaffDepartment($bookkeeping['stid']);
                                            if (!array_key_exists($departmentName, $mergeSupplierData)) {
                                                $mergeSupplierData[$departmentName] = [
                                                    'subjectId' => $subject['id'],
                                                    'orderMoney' => $orderMoney,
                                                    'taxMoney' => $taxMoney,
                                                    'moneyWithNoTax' => $moneyWithNoTax,
                                                ];
                                            } else {
                                                $mergeSupplierData[$departmentName] = [
                                                    'subjectId' => $subject['id'],
                                                    'orderMoney' => $orderMoney + $mergeSupplierData[$departmentName]['orderMoney'],
                                                    'taxMoney' => $taxMoney + $mergeSupplierData[$departmentName]['taxMoney'],
                                                    'moneyWithNoTax' => $moneyWithNoTax + $mergeSupplierData[$departmentName]['moneyWithNoTax'],
                                                ];
                                            }
                                            break;
                                        case '项目':
                                            $assitItemCode['projectCode'] = $assitItemCode['projectCode'] ?? '';
                                            if (!array_key_exists($assitItemCode['projectCode'], $mergeSupplierData)) {
                                                $mergeSupplierData[$assitItemCode['projectCode']] = [
                                                    'subjectId' => $subject['id'],
                                                    'orderMoney' => $orderMoney,
                                                    'taxMoney' => $taxMoney,
                                                    'moneyWithNoTax' => $moneyWithNoTax,
                                                ];
                                            } else {
                                                $mergeSupplierData[$assitItemCode['projectCode']] = [
                                                    'subjectId' => $subject['id'],
                                                    'orderMoney' => $orderMoney + $mergeSupplierData[$assitItemCode['projectCode']]['orderMoney'],
                                                    'taxMoney' => $taxMoney + $mergeSupplierData[$assitItemCode['projectCode']]['taxMoney'],
                                                    'moneyWithNoTax' => $moneyWithNoTax + $mergeSupplierData[$assitItemCode['projectCode']]['moneyWithNoTax'],
                                                ];
                                            }
                                            break;
                                        case '现金流':
                                            $assitItemCode['cashCode'] = $assitItemCode['cashCode'] ?? '';
                                            if (!array_key_exists($assitItemCode['cashCode'], $mergeSupplierData)) {
                                                $mergeSupplierData[$assitItemCode['cashCode']] = [
                                                    'subjectId' => $subject['id'],
                                                    'orderMoney' => $orderMoney,
                                                    'taxMoney' => $taxMoney,
                                                    'moneyWithNoTax' => $moneyWithNoTax,
                                                ];
                                            } else {
                                                $mergeSupplierData[$assitItemCode['cashCode']] = [
                                                    'subjectId' => $subject['id'],
                                                    'orderMoney' => $orderMoney + $mergeSupplierData[$assitItemCode['cashCode']]['orderMoney'],
                                                    'taxMoney' => $taxMoney + $mergeSupplierData[$assitItemCode['cashCode']]['taxMoney'],
                                                    'moneyWithNoTax' => $moneyWithNoTax + $mergeSupplierData[$assitItemCode['cashCode']]['moneyWithNoTax'],
                                                ];
                                            }
                                            break;
                                    }
                                }
                            } else {
                                foreach ($assitItemArr as $assitItem) {
                                    switch ($assitItem) {
                                        case '供应商':
                                            if (!array_key_exists($bookkeeping['supplierId'], $daiMergeSupplierData)) {
                                                $daiMergeSupplierData[$bookkeeping['supplierId']] = [
                                                    'orderMoney' => $orderMoney,
                                                    'taxMoney' => $taxMoney,
                                                    'moneyWithNoTax' => $moneyWithNoTax,
                                                    'subjectId' => $subject['id'],
                                                ];
                                            } else {
                                                $daiMergeSupplierData[$bookkeeping['supplierId']] = [
                                                    'orderMoney' => $orderMoney + $daiMergeSupplierData[$bookkeeping['supplierId']]['orderMoney'],
                                                    'taxMoney' => $taxMoney + $daiMergeSupplierData[$bookkeeping['supplierId']]['taxMoney'],
                                                    'moneyWithNoTax' => $moneyWithNoTax + $daiMergeSupplierData[$bookkeeping['supplierId']]['moneyWithNoTax'],
                                                    'subjectId' => $subject['id'],
                                                ];
                                            }
                                            break;
                                        case '客户':
                                            if (!array_key_exists($bookkeeping['khid'], $daiMergeSupplierData)) {
                                                $daiMergeSupplierData[$bookkeeping['khid']] = [
                                                    'orderMoney' => $orderMoney,
                                                    'taxMoney' => $taxMoney,
                                                    'moneyWithNoTax' => $moneyWithNoTax,
                                                    'subjectId' => $subject['id'],
                                                ];
                                            } else {
                                                $daiMergeSupplierData[$bookkeeping['khid']] = [
                                                    'orderMoney' => $orderMoney + $daiMergeSupplierData[$bookkeeping['khid']]['orderMoney'],
                                                    'taxMoney' => $taxMoney + $daiMergeSupplierData[$bookkeeping['khid']]['taxMoney'],
                                                    'moneyWithNoTax' => $moneyWithNoTax + $daiMergeSupplierData[$bookkeeping['khid']]['moneyWithNoTax'],
                                                    'subjectId' => $subject['id'],
                                                ];
                                            }
                                            break;
                                        case '员工':
                                            $bookkeeping['stid'] = $bookkeeping['stid'] ?? '';
                                            if (!array_key_exists($bookkeeping['stid'], $daiMergeSupplierData)) {
                                                $daiMergeSupplierData[$bookkeeping['stid']] = [
                                                    'orderMoney' => $orderMoney,
                                                    'taxMoney' => $taxMoney,
                                                    'moneyWithNoTax' => $moneyWithNoTax,
                                                    'subjectId' => $subject['id'],
                                                ];
                                            } else {
                                                $daiMergeSupplierData[$bookkeeping['stid']] = [
                                                    'orderMoney' => $orderMoney + $daiMergeSupplierData[$bookkeeping['stid']]['orderMoney'],
                                                    'taxMoney' => $taxMoney + $daiMergeSupplierData[$bookkeeping['stid']]['taxMoney'],
                                                    'moneyWithNoTax' => $moneyWithNoTax + $daiMergeSupplierData[$bookkeeping['stid']]['moneyWithNoTax'],
                                                    'subjectId' => $subject['id'],
                                                ];
                                            }
                                            break;
                                        case '部门':
                                            $bookkeeping['stid'] = $bookkeeping['stid'] ?? '';
                                            $departmentName = self::findStaffDepartment($bookkeeping['stid']);
                                            if (!array_key_exists($departmentName, $daiMergeSupplierData)) {
                                                $daiMergeSupplierData[$departmentName] = [
                                                    'orderMoney' => $orderMoney,
                                                    'taxMoney' => $taxMoney,
                                                    'moneyWithNoTax' => $moneyWithNoTax,
                                                    'subjectId' => $subject['id'],
                                                ];
                                            } else {
                                                $daiMergeSupplierData[$departmentName] = [
                                                    'orderMoney' => $orderMoney + $daiMergeSupplierData[$departmentName]['orderMoney'],
                                                    'taxMoney' => $taxMoney + $daiMergeSupplierData[$departmentName]['taxMoney'],
                                                    'moneyWithNoTax' => $moneyWithNoTax + $daiMergeSupplierData[$departmentName]['moneyWithNoTax'],
                                                    'subjectId' => $subject['id'],
                                                ];
                                            }
                                            break;
                                        case '项目':
                                            $assitItemCode['projectCode'] = $assitItemCode['projectCode'] ?? '';
                                            if (!array_key_exists($assitItemCode['projectCode'], $daiMergeSupplierData)) {
                                                $daiMergeSupplierData[$assitItemCode['projectCode']] = [
                                                    'orderMoney' => $orderMoney,
                                                    'taxMoney' => $taxMoney,
                                                    'moneyWithNoTax' => $moneyWithNoTax,
                                                    'subjectId' => $subject['id'],
                                                ];
                                            } else {
                                                $daiMergeSupplierData[$assitItemCode['projectCode']] = [
                                                    'orderMoney' => $orderMoney + $daiMergeSupplierData[$assitItemCode['projectCode']]['orderMoney'],
                                                    'taxMoney' => $taxMoney + $daiMergeSupplierData[$assitItemCode['projectCode']]['taxMoney'],
                                                    'moneyWithNoTax' => $moneyWithNoTax + $daiMergeSupplierData[$assitItemCode['projectCode']]['moneyWithNoTax'],
                                                    'subjectId' => $subject['id'],
                                                ];
                                            }
                                            break;
                                        case '现金流':
                                            $assitItemCode['cashCode'] = $assitItemCode['cashCode'] ?? '';
                                            if (!array_key_exists($assitItemCode['cashCode'], $daiMergeSupplierData)) {
                                                $daiMergeSupplierData[$assitItemCode['cashCode']] = [
                                                    'orderMoney' => $orderMoney,
                                                    'taxMoney' => $taxMoney,
                                                    'moneyWithNoTax' => $moneyWithNoTax,
                                                    'subjectId' => $subject['id'],
                                                ];
                                            } else {
                                                $daiMergeSupplierData[$assitItemCode['cashCode']] = [
                                                    'orderMoney' => $orderMoney + $daiMergeSupplierData[$assitItemCode['cashCode']]['orderMoney'],
                                                    'taxMoney' => $taxMoney + $daiMergeSupplierData[$assitItemCode['cashCode']]['taxMoney'],
                                                    'moneyWithNoTax' => $moneyWithNoTax + $daiMergeSupplierData[$assitItemCode['cashCode']]['moneyWithNoTax'],
                                                    'subjectId' => $subject['id'],
                                                ];
                                            }
                                            break;
                                        default:
                                            break;
                                    }
                                }
                            }

                        } else {
                            if ($array['direction'] == '借') {
                                if (!array_key_exists($subject['id'], $mergeSupplierData)) {
                                    $mergeSupplierData[$subject['id']] = [
                                        'subjectId' => $subject['id'],
                                        'orderMoney' => $orderMoney,
                                        'taxMoney' => $taxMoney,
                                        'moneyWithNoTax' => $moneyWithNoTax,
                                    ];
                                } else {
                                    $mergeSupplierData[$subject['id']] = [
                                        'subjectId' => $subject['id'],
                                        'orderMoney' => $orderMoney + $mergeSupplierData[$subject['id']]['orderMoney'],
                                        'taxMoney' => $taxMoney + $mergeSupplierData[$subject['id']]['taxMoney'],
                                        'moneyWithNoTax' => $moneyWithNoTax + $mergeSupplierData[$subject['id']]['moneyWithNoTax'],
                                    ];
                                }
                            } else {
                                if (!array_key_exists($subject['id'], $daiMergeSupplierData)) {
                                    $daiMergeSupplierData[$subject['id']] = [
                                        'subjectId' => $subject['id'],
                                        'orderMoney' => $orderMoney,
                                        'taxMoney' => $taxMoney,
                                        'moneyWithNoTax' => $moneyWithNoTax,
                                    ];
                                } else {
                                    $daiMergeSupplierData[$subject['id']] = [
                                        'subjectId' => $subject['id'],
                                        'orderMoney' => $orderMoney + $daiMergeSupplierData[$subject['id']]['orderMoney'],
                                        'taxMoney' => $taxMoney + $daiMergeSupplierData[$subject['id']]['taxMoney'],
                                        'moneyWithNoTax' => $moneyWithNoTax + $daiMergeSupplierData[$subject['id']]['moneyWithNoTax'],
                                    ];
                                }
                            }
                        }
                    }

                }

            }
        }
        $templateDetail['array'] = $templateDetail['array'] ?? [];
        $template['preSummary'] = $template['preSummary'] ?? '';
        $template['suffixSummary'] = $template['suffixSummary'] ?? '';
        $bookkeeping['orderNo'] = $bookkeeping['orderNo'] ?? '';
        $bookkeeping['stid'] = $bookkeeping['stid'] ?? '';
        $maxDays = date('t', $maxStrtotime);
        $recordDate = date('Y-m-' . sprintf('%02d', $maxDays), $maxStrtotime);
        $recordMonth = date('Y-m', $maxStrtotime);
        $money =$daiTotalMoney =  0;
        $liItem = '';
        foreach ($templateDetail['array'] as $array) {
            $summary = $template['preSummary'] ? '_' . $template['preSummary'] : '';
            $summary .= '_' . $recordMonth . $typeOne . '合并';
            $summary .= $template['suffixSummary'] ? '_' . $template['suffixSummary'] : '';
            $summary = ltrim($summary, '_');

            if (in_array($array['rule'], ['明细价税合计', '明细金额', '明细税额'])) {
                $subjectMerge = [];//合并科目
                $subjectNotMerge = [];//不合并科目
                foreach ($mergeData as $child) {
                    $child['materialId'] = $child['materialId'] ?? '';
                    $material = query('material', "id = '$child[materialId]'");
                    $subject = $this->subjectSource($array, $material['id']);
                    if (in_array($typeOne, ['费用报销单', '核销单'])) {
                        //费用报销中存在不同类型对应不同科目的情况
                        $business_voucher_subject = query('business_voucher_subject', "orderId = '$array[id]' and type = '$child[type]' and draft = 0 order by subjectCode asc");
                        if (!empty($business_voucher_subject['id'])) {
                            $subject = query('subject', "id = '$business_voucher_subject[subjectId]'");
                        }
                    }
                    if ($array['subjectSource'] == 'voucherModel' && in_array($typeOne, $this->accountMainReceipt)) {
                        //不同类型对应不同科目的情况
                        $business_voucher_subject = query('business_voucher_subject', "orderId = '$array[id]' and type = '$child[project]' and draft = 0 order by subjectCode asc");
                        if (!empty($business_voucher_subject['id'])) {
                            $subject = query('subject', "id = '$business_voucher_subject[subjectId]'");
                        }
                    }
                    if ($array['subjectSource'] == 'account' && in_array($typeOne, $this->accountReceipt)) {
                        $account = query('account', "id = '$child[acid]'");
                        $subject = query('subject', "id = '$account[subjectId]'");
                    }
                    $subjectId = $subject['id'];
                    $subjectName = $subject['code'] . ' ' . subjectClass::getName($subject['id']);
                    $assitItemArr = [];
                    if ($subject['assistAccount'] == '是') {
                        $assitItemArr = explode(',', $subject['assistAccountIds']);
                    }
                    $ruleMoney = match ($array['rule']) {
                        '明细价税合计' => $child['money'],
                        '明细金额' => $child['moneyWithOutTax'],
                        '明细税额' => $child['taxMoney'],
                        default => 0,
                    };
                    $ruleMoney = self::formattingFigure($ruleMoney);
                    if ($array['direction'] == '借') {
                        $money += $ruleMoney;
                        $jieMoney = $ruleMoney;
                        $daiMoney = 0;
                    } else {
                        $daiTotalMoney+=$ruleMoney;
                        $daiMoney = $ruleMoney;
                        $jieMoney = 0;
                    }

                    if (in_array('存货', $assitItemArr)) {
                        $material = query('material', "id = '$child[materialId]'");
                        $subjectLineName = $subjectName . '_' . $material['name'];
                        $subjectNotMerge[] = [
                            'summary' => $summary,
                            'subjectLineName' => $subjectLineName,
                            'jieMoney' => $jieMoney,
                            'daiMoney' => $daiMoney,
                        ];
                    } else {
                        $jMoney = $subjectMerge[$subjectId]['jieMoney'] ?? 0;
                        $dMoney = $subjectMerge[$subjectId]['daiMoney'] ?? 0;

                        $subjectMerge[$subjectId] = [
                            'summary' => $summary,
                            'jieMoney' => $jieMoney + $jMoney,
                            'daiMoney' => $daiMoney + $dMoney
                        ];
                    }

                }
                foreach ($subjectNotMerge as $value) {
                    $value['jieMoney'] = $value['jieMoney'] ?: '';
                    $value['daiMoney'] = $value['daiMoney'] ?: '';
                    $liItem .= <<<eof
                                <div class="voucher-line">
                                    <div class="voucher-line-summary">{$value['summary']}</div>
                                    <div class="voucher-line-subject">{$value['subjectLineName']}</div>
                                    <div class="voucher-line-jie">{$value['jieMoney']}</div>
                                    <div class="voucher-line-dai">{$value['daiMoney']}</div>
                                </div>
eof;
                }
                foreach ($subjectMerge as $subjectId => $value) {
                    $value['jieMoney'] = $value['jieMoney'] ?: '';
                    $value['daiMoney'] = $value['daiMoney'] ?: '';
                    $subject = query('subject', "id = '$subjectId'");
                    $subjectName = $subject['code'] . ' ' . subjectClass::getName($subject['id']);
                    $liItem .= <<<eof
                                <div class="voucher-line">
                                    <div class="voucher-line-summary">{$value['summary']}</div>
                                    <div class="voucher-line-subject">{$subjectName}</div>
                                    <div class="voucher-line-jie">{$value['jieMoney']}</div>
                                    <div class="voucher-line-dai">{$value['daiMoney']}</div>
                                </div>
eof;
                }
            } else {
                if ($array['direction'] == '贷') {
                    $loopArr = $daiMergeSupplierData;
                } else {
                    $loopArr = $mergeSupplierData;
                }
                $subjectText = '';
                $subjectRule = $this->subjectSource($array);
                if ($array['subjectSource'] == 'account' && in_array($typeOne, $this->accountMainReceipt)) {
                    //账户在主表上的时候
                    $subjectRule = query('subject',"id = '$bookkeeping[acid]'");
                }
                foreach ($loopArr as $supplierId => $value) {
                    if ($subjectRule['id'] != $value['subjectId']){
                        continue;
                    }
                    $subject = query('subject', "id = '$value[subjectId]'");
                    if ($subject['assistAccount'] == '是') {
                        $assitItemArr = explode(',', $subject['assistAccountIds']);
                        foreach ($assitItemArr as $assitItem) {
                            switch ($assitItem) {
                                case '供应商':
                                    $supplier = query('supplier', "id = '$supplierId'");
                                    $subjectText = '_' . $supplier['name'];
                                    break;
                                case '客户':
                                    $kehu = query('kehu', "khid = '$supplierId'");
                                    $subjectText = '_' . $kehu['companyName'];
                                    break;
                                case '员工':
                                    $subjectText = '_' . self::findStaff($supplierId);
                                    break;
                                case '部门':
                                    $subjectText = '_' . $supplierId;
                                    break;
                                case '项目':
                                    $project = query('project', "orderNo = '$supplierId' and coid = '" . self::$coid . "'");
                                    $subjectText = '_' . $project['name'];
                                    break;
                                case '现金流':
                                    $cash = query('cash', "code = '$supplierId' and coid = '" . self::$coid . "'");
                                    $subjectText = '_' . $cash['name'];
                                    break;
                            }
                        }
                    } else {
                        //没有辅助核算
                        if (!$supplierId) {
                            continue;
                        }
                    }
                    $ruleMoney = match ($array['rule']) {
                        '汇总价税合计' => $value['orderMoney'],
                        '汇总金额' => $value['moneyWithNoTax'],
                        '汇总税额' => $value['taxMoney'],
                        default => 0,
                    };
                    $ruleMoney = self::formattingFigure($ruleMoney);
                    if ($array['direction'] == '借') {
                        $money += $ruleMoney;
                        $jieMoney = $ruleMoney;
                        $daiMoney = '';
                    } else {
                        $daiTotalMoney+=$ruleMoney;
                        $daiMoney = $ruleMoney;
                        $jieMoney = '';
                    }
                    $subjectName = $subject['code'] . ' ' . subjectClass::getName($subject['id']);
                    $subjectLineName = $subjectName . $subjectText;
                    $liItem .= <<<eof
                                <div class="voucher-line">
                                    <div class="voucher-line-summary">$summary</div>
                                    <div class="voucher-line-subject">$subjectLineName</div>
                                    <div class="voucher-line-jie">$jieMoney</div>
                                    <div class="voucher-line-dai">$daiMoney</div>
                                </div>
eof;
                }
            }
        }
        $bigMoney = voucherEditClass::num_to_rmb($money);
        $voucherWord['id'] = $voucherWord['id'] ?? '';
        $voucherNo = voucherEditClass::voucherNo($voucherWord['id'], $recordDate);
        return <<<eof
 <div class="content-box">
                    <div class="left-content-box">
                        $lefItem
                    </div>
                    <div class="right-content-box">
                         <div class="right-box-item">
                           <div class="item-li-no"><div>日期：{$bookkeeping['recordDate']}<span style="margin-left: 20px">凭证字号：{$voucherWord['word']}-{$voucherNo}</span> <span style="margin-left: 20px">附件：0</span></div><div class="voucher-line-subject"></div><div class="voucher-line-jie">借方</div><div class="voucher-line-dai">贷方</div> </div>
                            <div class="item-li-content right-item">
                                $liItem
                                <div class="voucher-line">
                                    <div class="voucher-line-summary">合  计：$bigMoney</div>
                                    <div class="voucher-line-subject"></div>
                                    <div class="voucher-line-jie">$money</div>
                                    <div class="voucher-line-dai">$daiTotalMoney</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
eof;
    }

    /**
     * 账户合并
     * @param $result
     * @param $append
     * @param $typeOne
     */
    public function accountMergArray($result, $append, $typeOne)
    {
        if (in_array($typeOne, $this->accountReceipt)) {
            $acidArray = array_column($result, 'acid');
            if (!is_array($acidArray) || !in_array($append['acid'], $acidArray)) {
                //账户合并
                $result[] = [
                    'acid' => $append['acid'],
                    'money' => $append['money'],
                    'moneyWithOutTax' => $append['moneyWithOutTax'],
                    'taxMoney' => $append['taxMoney'],
                ];
            } else {
                foreach ($result as $index => $array) {
                    //账户合并
                    if ($array['acid'] == $append['acid']) {
                        $result[$index] = [
                            'acid' => $append['acid'],
                            'money' => $append['money'] + $array['money'],
                            'moneyWithOutTax' => $append['moneyWithOutTax'] + $array['moneyWithOutTax'],
                            'taxMoney' => $append['taxMoney'] + $array['taxMoney'],
                        ];
                        break;
                    }
                }
            }
        } elseif (in_array($typeOne, $this->accountMainReceipt)) {
            $acidArray = array_column($result, 'project');
            if (!is_array($acidArray) || !in_array($append['project'], $acidArray)) {
                //账户合并
                $result[] = [
                    'project' => $append['project'],
                    'money' => $append['money'],
                    'moneyWithOutTax' => $append['moneyWithOutTax'],
                    'taxMoney' => $append['taxMoney'],
                ];
            } else {
                foreach ($result as $index => $array) {
                    //账户合并
                    if ($array['project'] == $append['project']) {
                        $result[$index] = [
                            'project' => $append['project'],
                            'money' => $append['money'] + $array['money'],
                            'moneyWithOutTax' => $append['moneyWithOutTax'] + $array['moneyWithOutTax'],
                            'taxMoney' => $append['taxMoney'] + $array['taxMoney'],
                        ];
                        break;
                    }
                }
            }
        } else {
            $append['materialId'] = $append['materialId'] ?? '';
            $material = query('material', "id = '$append[materialId]'");
            $append['unit'] = $append['unit'] ?? $material['unit'];
            $acidArray = array_column($result, 'materialId');
            if (!is_array($acidArray) || !in_array($append['materialId'], $acidArray)) {
                //物料合并
                $result[] = [
                    'materialId' => $append['materialId'],
                    'money' => $append['money'],
                    'moneyWithOutTax' => $append['moneyWithOutTax'],
                    'taxMoney' => $append['taxMoney'],
                    'num' => machiningOrderClass::material_num_change($append['materialId'], $append['unit'], $material['unit'])
                ];
            } else {
                foreach ($result as $index => $array) {
                    //物料合并
                    if ($array['materialId'] == $append['materialId']) {
                        $result[$index] = [
                            'materialId' => $append['materialId'],
                            'money' => $append['money'] + $array['money'],
                            'moneyWithOutTax' => $append['moneyWithOutTax'] + $array['moneyWithOutTax'],
                            'taxMoney' => $append['taxMoney'] + $array['taxMoney'],
                            'num' => $array['num'] + machiningOrderClass::material_num_change($append['materialId'], $append['unit'], $material['unit'])
                        ];
                        break;
                    }
                }
            }
        }
        return $result;
    }

    /**
     * 不合并凭证
     * @param $listArr
     * @param $typeOne
     * @param $typeTwo
     * @return string
     */
    public function previewVoucher($listArr, $typeOne, $typeTwo): string
    {
        $html = '';
        $orderMoney = 0;//单据金额
        $moneyWithNoTax = 0;//未税金额
        $taxMoney = 0;//税额
        foreach ($listArr as $orderId) {
            switch ($typeTwo) {
//                case '领料单':
//                case '退料单':
//                    $bookkeeping = query('pick_order', "id = '$orderId'");
//                    $children = find(array('table' => 'pick_order_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
//                    $total = find(array('table' => 'pick_order_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money"));
//                    $orderMoney = self::formattingFigure($total['money']);
//                    $moneyWithNoTax = self::formattingFigure($total['money']);
//                    $taxMoney = 0;
//                    break;
                case '采购入库':
                    $bookkeeping = query('purchase_get_order', "id = '$orderId'");
                    $children = find(array('table' => 'purchase_get_order_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'purchase_get_order_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money,ifnull(sum(moneyWithOutTax),0) as moneyWithOutTax,ifnull(sum(taxMoney),0) as taxMoney"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['moneyWithOutTax'];
                    $taxMoney = $total['taxMoney'];
                    break;
                case '采购退货':
                    $bookkeeping = query('purchase_refund_order', "id = '$orderId'");
                    $children = find(array('table' => 'purchase_refund_order_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'purchase_refund_order_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money,ifnull(sum(moneyWithOutTax),0) as moneyWithOutTax,ifnull(sum(taxMoney),0) as taxMoney"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['moneyWithOutTax'];
                    $taxMoney = $total['taxMoney'];
                    break;
                case '销售出库':
                    $bookkeeping = query('sale_send_order', "id = '$orderId'");
                    $children = find(array('table' => 'sale_send_order_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'sale_send_order_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money,ifnull(sum(moneyWithOutTax),0) as moneyWithOutTax,ifnull(sum(taxMoney),0) as taxMoney"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['moneyWithOutTax'];
                    $taxMoney = $total['taxMoney'];
                    break;
                case '销售退货':
                    $bookkeeping = query('sale_refund_order', "id = '$orderId'");
                    $children = find(array('table' => 'sale_refund_order_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'sale_refund_order_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money,ifnull(sum(moneyWithOutTax),0) as moneyWithOutTax,ifnull(sum(taxMoney),0) as taxMoney"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['moneyWithOutTax'];
                    $taxMoney = $total['taxMoney'];
                    break;
                case '采购暂估单':
                    $bookkeeping = query('purchase_esti_order', "id = '$orderId'");
                    $children = find(array('table' => 'purchase_esti_order_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'purchase_esti_order_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money,ifnull(sum(moneyWithOutTax),0) as moneyWithOutTax,ifnull(sum(taxMoney),0) as taxMoney"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['moneyWithOutTax'];
                    $taxMoney = $total['taxMoney'];
                    break;
                case '收入单':
                case '费用单':
                    $bookkeeping = query('other_receipt', "id = '$orderId'");
                    if ($bookkeeping['target'] == '供应商') {
                        $bookkeeping['supplierId'] = $bookkeeping['targetId'];
                    } else {
                        //默认客户
                        $bookkeeping['khid'] = $bookkeeping['targetId'];
                    }
                    $children = find(array('table' => 'other_receipt_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'other_receipt_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['money'];
                    $taxMoney = 0;
                    break;
                case '付款单':
                case '收款单':
                    $bookkeeping = query('receipt', "id = '$orderId'");
                    if ($bookkeeping['target'] == '供应商') {
                        $bookkeeping['supplierId'] = $bookkeeping['targetId'];
                    } else {
                        //默认客户
                        $bookkeeping['khid'] = $bookkeeping['targetId'];
                    }
                    $children = find(array('table' => 'receipt_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'receipt_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['money'];
                    $taxMoney = 0;
                    break;
                case '销项发票':
                case '进项发票':
                    $bookkeeping = query('invoice_open', "id = '$orderId'");
                    if ($bookkeeping['type'] == '收票') {
                        $bookkeeping['supplierId'] = $bookkeeping['khid'];
                    }
                    $children = find(array('table' => 'invoice_open_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $orderMoney = $bookkeeping['money'];
                    $moneyWithNoTax = $bookkeeping['unTaxMoney'];
                    $taxMoney = $bookkeeping['taxMoney'];
                    $bookkeeping['orderNo'] = $bookkeeping['invoiceNo'];
                    $bookkeeping['recordDate'] = $bookkeeping['invoiceDate'];
                    break;
                case '领料单':
                case '退料单':
                case '生产入库单':
                case '出库':
                case '退货':
                case '盘盈':
                case '其他入库':
                case '盘亏':
                case '其他出库':
                    $bookkeeping = query('inventory_changes', "id = '$orderId'");
                    $children = find(array('table' => 'inventory_changes_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'inventory_changes_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money"));
                    $total['money'] = self::formattingFigure($total['money']);
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['money'];
                    $taxMoney = 0;
                    $bookkeeping['recordDate'] = $bookkeeping['confirmTime']?date('Y-m-d',strtotime($bookkeeping['confirmTime'])):date('Y-m-d',strtotime($bookkeeping['time']));
                    break;
                case '成本调整单':
                    $bookkeeping = query('inventory_cost', "id = '$orderId'");
                    $children = find(array('table' => 'inventory_cost_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'inventory_cost_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(orderPrice),0) as money"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['money'];
                    $taxMoney = 0;
                    $bookkeeping['recordDate'] = $bookkeeping['orderDate'];
                    break;
                case '组装单':
                    $bookkeeping = query('inventory_assembly', "id = '$orderId'");
                    $children = find(array('table' => 'inventory_assembly_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'inventory_assembly_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(price*num),0) as money"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['money'];
                    $taxMoney = 0;
                    $bookkeeping['recordDate'] = $bookkeeping['orderDate'];
                    break;
                case '拆卸单':
                    $bookkeeping = query('inventory_dismantle', "id = '$orderId'");
                    $children = find(array('table' => 'inventory_dismantle_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                    $total = find(array('table' => 'inventory_dismantle_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(price*num),0) as money"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['money'];
                    $taxMoney = 0;
                    $bookkeeping['recordDate'] = $bookkeeping['orderDate'];
                    break;
                case '转账单':
                    $bookkeeping = query('transfer_order', "id = '$orderId'");
                    $children['array'] = [];
                    $total = find(array('table' => 'transfer_order', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money"));
                    $orderMoney = $total['money'];
                    $moneyWithNoTax = $total['money'];
                    $taxMoney = 0;
                    break;
                default:
                    if ($typeOne == '费用报销单') {
                        $bookkeeping = query('cost', "id = '$orderId'");
                        $children = find(array('table' => 'cost_record', 'where' => "costId = '$bookkeeping[id]' order by time asc"));
                        $total = find(array('table' => 'cost_record', 'single' => "costId = '$orderId'", 'field' => "ifnull(sum(money),0) as money"));
                        $orderMoney = $total['money'];
                        $moneyWithNoTax = $total['money'];
                        $taxMoney = 0;
                    } elseif ($typeOne == '核销单') {
                        $bookkeeping = query('write_off_order_detail', "id = '$orderId'");
                        $children = find(array('table' => 'write_off_order_detail', 'where' => "orderId = '$bookkeeping[id]' order by list asc"));
                        $total = find(array('table' => 'write_off_order_detail', 'single' => "orderId = '$orderId'", 'field' => "ifnull(sum(money),0) as money"));
                        $orderMoney = $total['money'];
                        $moneyWithNoTax = $total['money'];
                        $taxMoney = 0;
                    } else {
                        $bookkeeping = [
                            'orderNo' => '',
                            'recordDate' => '',
                        ];
                        $children['array'] = [];
                    }
                    break;
            }

            if (empty($bookkeeping['voucherId'])) {
                $template = query('business_voucher_template', "coid = '" . self::$coid . "' and type = '$typeTwo' and defaultTemplate = 1");
                $templateDetail = find(array('table' => 'business_voucher_template_detail', 'where' => "orderId = '$template[id]' order by list asc"));
                $voucherWord = query('voucher_word', "id = '$template[wordId]'");
                $recordMonth = date('Y-m', strtotime($bookkeeping['recordDate']));
                $voucherIndex[$voucherWord['id']][$recordMonth] = $voucherIndex[$voucherWord['id']][$recordMonth] ?? 1;
                $lefItem = <<<eof
                        <div class="left-box-item">
                            <div class="item-li-no">单据编号：$bookkeeping[orderNo]</div>
                            <div class="item-li-content">
                                <div>日期：{$bookkeeping['recordDate']}</div>
                                <div>业务类型：{$typeTwo}</div>
                                <div>单据金额：{$orderMoney}</div>
                            </div>
                        </div>
eof;
                $liItem = '';
                $money = $daiTotalMoney = 0;
                foreach ($templateDetail['array'] as $array) {
                    $assitItemCode = $array['assitItem'] ? json_decode($array['assitItem'], true) : [];
                    $summaryField = $template['summary'] ? json_decode($template['summary'], true) : [];
                    $summary = $template['preSummary'] ? '_' . $template['preSummary'] : '';
                    if (isset($summaryField['orderNo']) && !empty($summaryField['orderNo'])) {
                        $summary .= '_' . $bookkeeping['orderNo'];
                    }
                    if (isset($summaryField['department']) && !empty($summaryField['department'])) {
                        $summary .= '_' . self::findStaffDepartment($bookkeeping['stid']);
                    }
                    if (isset($summaryField['staff']) && !empty($summaryField['staff'])) {
                        $summary .= '_' . self::findStaff($bookkeeping['stid']);
                    }
                    $summary .= $template['suffixSummary'] ? '_' . $template['suffixSummary'] : '';
                    $summary = ltrim($summary, '_');
                    if (in_array($array['rule'], ['明细价税合计', '明细金额', '明细税额'])) {
                        foreach ($children['array'] as $child) {
                            $child['materialId'] = $child['materialId'] ?? '';
                            $material = query('material', "id = '$child[materialId]'");
                            $subjectText = '';
                            $subject = $this->subjectSource($array, $material['id']);

                            if (in_array($typeOne, ['费用报销单', '核销单'])) {
                                $child['classify'] = $child['classify'] ?? '';
                                $child['type'] = $child['type'] ?? $child['classify'];
                                //费用报销中存在不同类型对应不同科目的情况
                                $business_voucher_subject = query('business_voucher_subject', "orderId = '$array[id]' and type = '$child[type]' and draft = 0 order by subjectCode asc");
                                if (!empty($business_voucher_subject['id'])) {
                                    $subject = query('subject', "id = '$business_voucher_subject[subjectId]'");
                                }
                            }
                            if (in_array($typeOne, $this->accountMainReceipt)) {
                                //不同类型对应不同科目的情况
                                $business_voucher_subject = query('business_voucher_subject', "orderId = '$array[id]' and type = '$child[project]' and draft = 0 order by subjectCode asc");
                                if (!empty($business_voucher_subject['id'])) {
                                    $subject = query('subject', "id = '$business_voucher_subject[subjectId]'");
                                }
                            }
                            if ($array['subjectSource'] == 'account' && in_array($typeOne, $this->accountReceipt)) {
                                //账户在明细上的时候
                                $account = query('account', "id = '$child[acid]'");
                                $subject = query('subject', "id = '$account[subjectId]'");
                            }

                            $subjectName = $subject['code'] . ' ' . subjectClass::getName($subject['id']);
                            $assitItemArr = explode(',', $subject['assistAccountIds']);
                            foreach ($assitItemArr as $assitItem) {
                                switch ($assitItem) {
                                    case '供应商':
                                        $supplier = query('supplier', "id = '$bookkeeping[supplierId]'");
                                        $subjectText .= '_' . $supplier['name'];
                                        break;
                                    case '客户':
                                        $kehu = query('kehu', "khid = '$bookkeeping[khid]'");
                                        $subjectText .= '_' . $kehu['companyName'];
                                        break;
                                    case '员工':
                                        $subjectText .= '_' . self::findStaff($bookkeeping['stid']);
                                        break;
                                    case '部门':
                                        $subjectText .= '_' . self::findStaffDepartment($bookkeeping['stid']);
                                        break;
                                    case '项目':
                                        $assitItemCode['projectCode'] = $assitItemCode['projectCode'] ?? '';
                                        $project = query('project', "orderNo = '$assitItemCode[projectCode]' and coid = '" . self::$coid . "'");
                                        $subjectText .= '_' . $project['name'];
                                        break;
                                    case '现金流':
                                        $assitItemCode['cashCode'] = $assitItemCode['cashCode'] ?? '';
                                        $cash = query('cash', "code = '$assitItemCode[cashCode]' and coid = '" . self::$coid . "'");
                                        $subjectText .= '_' . $cash['name'];
                                        break;
                                }
                            }

                            $ruleMoney = match ($array['rule']) {
                                '明细价税合计' => $child['money'] ?? 0,
                                '明细金额' => $child['moneyWithOutTax'] ?? 0,
                                '明细税额' => $child['taxMoney'] ?? 0,
                                default => 0,
                            };
                            $ruleMoney = self::formattingFigure($ruleMoney);
                            if ($array['direction'] == '借') {
                                $money += $ruleMoney;
                                $jieMoney = $ruleMoney;
                                $daiMoney = '';
                            } else {
                                $daiTotalMoney+=$ruleMoney;
                                $daiMoney = $ruleMoney;
                                $jieMoney = '';
                            }

                            $subjectLineName = $subjectName . $subjectText;
                            if ($material['id'] && in_array('存货', $assitItemArr)) {
                                $subjectLineName .= '-' . $material['name'];
                            }
                            $liItem .= <<<eof
                                <div class="voucher-line">
                                    <div class="voucher-line-summary">$summary</div>
                                    <div class="voucher-line-subject">$subjectLineName</div>
                                    <div class="voucher-line-jie">$jieMoney</div>
                                    <div class="voucher-line-dai">$daiMoney</div>
                                </div>
eof;
                        }
                    } else {
                        $subjectText = '';
                        $subject = $this->subjectSource($array);
                        if ($array['subjectSource'] == 'account' && in_array($typeOne, $this->accountMainReceipt)) {
                            //账户在明细上的时候
//                            $account = query('account', "id = '$bookkeeping[acid]'");
                            $subject = query('subject', "id = '$bookkeeping[acid]'");
                        }
                        $subjectName = $subject['code'] . ' ' . subjectClass::getName($subject['id']);
                        $assitItemArr = explode(',', $subject['assistAccountIds']);
                        foreach ($assitItemArr as $assitItem) {
                            switch ($assitItem) {
                                case '供应商':
                                    $supplier = query('supplier', "id = '$bookkeeping[supplierId]'");
                                    $subjectText .= '_' . $supplier['name'];
                                    break;
                                case '客户':
                                    $kehu = query('kehu', "khid = '$bookkeeping[khid]'");
                                    $subjectText .= '_' . $kehu['companyName'];
                                    break;
                                case '员工':
                                    $subjectText .= '_' . self::findStaff($bookkeeping['stid']);
                                    break;
                                case '部门':
                                    $subjectText .= '_' . self::findStaffDepartment($bookkeeping['stid']);
                                    break;
                                case '项目':
                                    $assitItemCode['projectCode'] = $assitItemCode['projectCode'] ?? '';
                                    $project = query('project', "orderNo = '$assitItemCode[projectCode]' and coid = '" . self::$coid . "'");
                                    $subjectText .= '_' . $project['name'];
                                    break;
                                case '现金流':
                                    $assitItemCode['cashCode'] = $assitItemCode['cashCode'] ?? '';
                                    $cash = query('cash', "code = '$assitItemCode[cashCode]' and coid = '" . self::$coid . "'");
                                    $subjectText .= '_' . $cash['name'];
                                    break;
                            }
                        }
                        $ruleMoney = match ($array['rule']) {
                            '汇总价税合计' => $orderMoney,
                            '汇总金额' => $moneyWithNoTax,
                            '汇总税额' => $taxMoney,
                            default => 0,
                        };
                        $ruleMoney = self::formattingFigure($ruleMoney);
                        if ($array['direction'] == '借') {
                            $money += $ruleMoney;
                            $jieMoney = $ruleMoney;
                            $daiMoney = '';
                        } else {
                            $daiTotalMoney+=$ruleMoney;
                            $daiMoney = $ruleMoney;
                            $jieMoney = '';
                        }
                        $subjectLineName = $subjectName . $subjectText;
                        $liItem .= <<<eof
                                <div class="voucher-line">
                                    <div class="voucher-line-summary">$summary</div>
                                    <div class="voucher-line-subject">$subjectLineName</div>
                                    <div class="voucher-line-jie">$jieMoney</div>
                                    <div class="voucher-line-dai">$daiMoney</div>
                                </div>
eof;
                    }
                }
                $bigMoney = voucherEditClass::num_to_rmb($money);
                $voucherNo = voucherEditClass::voucherNo($voucherWord['id'], $bookkeeping['recordDate'], $voucherIndex[$voucherWord['id']][$recordMonth]);
                $voucherIndex[$voucherWord['id']][$recordMonth] += 1;
                $rightItem = <<<eof
                        <div class="right-box-item">
                            <div class="item-li-no"><div>日期：{$bookkeeping['recordDate']}<span style="margin-left: 20px">凭证字号：{$voucherWord['word']}-{$voucherNo}</span> <span style="margin-left: 20px">附件：0</span></div><div class="voucher-line-subject"></div><div class="voucher-line-jie">借方</div><div class="voucher-line-dai">贷方</div> </div>
                            <div class="item-li-content right-item">
                                $liItem
                                <div class="voucher-line">
                                    <div class="voucher-line-summary">合  计：$bigMoney</div>
                                    <div class="voucher-line-subject"></div>
                                    <div class="voucher-line-jie">$money</div>
                                    <div class="voucher-line-dai">$daiTotalMoney</div>
                                </div>
                            </div>
                        </div>
eof;

                $html .= <<<eof
 <div class="content-box">
                    <div class="left-content-box">
                        $lefItem
                    </div>
                    <div class="right-content-box">
                        $rightItem
                    </div>
                </div>
eof;

            }
        }
        return $html;
    }

    /**
     * 根据不同的科目来源返回科目
     * @param $array
     * @param string $materialId
     * @return array|string
     */
    public function subjectSource($array, $materialId = '')
    {
        $material = query('material', "id = '$materialId'");
        switch ($array['subjectSource']) {
            case 'costSubject'://单据上物料的销售成本科目
                $subject = query('subject', "id = '$material[costSubjectId]'");
                break;
            case 'incomeSubject'://单据上物料的销售收入科目
                $subject = query('subject', "id = '$material[incomeSubjectId]'");
                break;
            case 'materialSubject'://单据上物料的存货科目
                $subject = query('subject', "id = '$material[subjectId]'");
                break;
            case 'account'://单据上账户的科目
                $account = query('account', "id = '$materialId'");
                $subject = query('subject', "id = '$account[subjectId]'");
                break;
            default:
                $subject = query('subject', "id = '$array[subjectId]'");
                break;
        }
        return $subject;
    }

    public function voucherCass()
    {
        return <<<eof
<style>
.voucher-line{line-height: 30px;padding-left: 5px;display: flex;justify-content: space-between}
.voucher-line:nth-child(n+2){border-top: 1px solid #dadada;}
.voucher-line-summary,.voucher-line-subject{
  flex: 1;
}

.voucher-line-summary{width: 500px;vertical-align: top}
.voucher-line-subject{width: 260px}
.voucher-line-jie{width: 91px;vertical-align: top;text-align: center}
.voucher-line-dai{width:91px;vertical-align: top;text-align: center}
.right-content-box{width: 100%;padding: 0 2px 0 10px}
.item-li-content{padding: 5px 0 5px 10px}
.left-box-item{width: 294px;border: 1px solid #dadada;margin-top: 10px}
.right-box-item{width: 100%;border: 1px solid #dadada;margin-top: 10px}
.content-box{display: flex;flex-direction: row}
.item-li-no{padding-left: 10px;height: 37px;line-height: 37px;background-color: #ededed}
.right-box-item .item-li-no{display: flex;justify-content: space-between}

.input-div{display: flex;flex-direction: row;background-color: #e6e6e6;padding: 5px 8px;border-radius: 2px;}
.input-div>div:nth-child(1){width: 300px}
.voucher-button{height: 38px}
.pageDiv,body{background-color: rgba(242, 242, 242, 1)}
.voucherTable{border-collapse: collapse;border-spacing: 0;text-align: center;font-size: 12px;margin-top: 10px}
.voucherTable td {border: 1px solid rgba(215, 215, 215, 1);border-spacing:0;padding: 0}
.voucherTable tr{cursor: pointer}
.bold{font-weight: bold}
.calendarBody{padding: 10px;background-color: white;}
.voucher-body{padding: 10px;background-color: rgba(242, 242, 242, 1)}
.right-item{padding-left: 0;padding-bottom: 0}
</style>
eof;

    }

    /**
     * 按钮
     * @return string
     */
    private function buttonGroup(): string
    {
        //控件
        $button = "";
        return $button;
    }

    /**
     * 领料单 以领料单为准
     * @param $oneMenuName
     * @param $secMenuName
     * @return string
     */
//    private function lingliaodan($oneMenuName, $secMenuName): string
//    {
//        //like查询数组
//        $like = array(
//            "orderNo" => $this->post('orderNo'),
//        );
//        $equal = [
//            "lineId" => $this->post('lineId'),
//            'coid' => self::$coid,
//            'state' => '已确认',
//        ];
//        $and = " and type = 0";
//        if ($this->post('createTime')) {
//            $and .= self::timeComparison($this->post('createTime'), 'date', 'recordDate');
//        }
//        //获取查询结果
//        $where = search([
//                "like" => $like,
//                '=' => $equal,
//            ]) . $and;
//        $page = $this->page([
//            "table" => 'pick_order',
//            "where" => $where,
//            "order" => "recordDate desc,orderNo desc",
//        ]);
//        //获取表头
//        $checkAll = $this->checkAll("listForm", "listId");
//        $tr[] = [$checkAll, '日期', '单据编号', '单据类型', '生产线', '单据金额', '制单人', '凭证字号'];
//        //获取记录
//        foreach ($page['array'] as $array) {
//            //列表控件
//            $dominate = array(
//                "box" => array("name" => "listId", "value" => $array['id']),
//            );
//            $money = find(array('table' => 'pick_order_detail', 'single' => "orderId = '$array[id]'", 'field' => "ifnull(sum(price*num),0) as total"))['total'];
//            $voucher = query('voucher', "id = '$array[voucherId]'");
//            $voucherWord = query('voucher_word', "id = '$voucher[voucherWordId]'");
//            $url = $array['voucherId'] ? (componentClass::urlTitle($voucherWord['word'] . '-' . $voucher['voucherNo'], root . 'control/adIndex.html?cla=voucherEdit&fun=room&id=' . $voucher['id'], '新增凭证')) : '';
//            //本列
//            $tr[] = [$this->dominate($dominate), $array['recordDate'], $array['orderNo'], '领料单', productLineClass::getName($array['lineId']), self::formattingFigure($money), self::findStaff($array['stid']), $url];
//        }
//        //返回
//        return $page['hint'] . $this->buttonGroup() . "<form name='listForm'>" . space . hidden(array('name' => 'typeOne', 'value' => $oneMenuName)) . hidden(array('name' => 'typeTwo', 'value' => $secMenuName)) . $this->tableMany(['tr' => $tr]) . "</form>" . $this->js() . $page['button'];
//    }


    /**
     * 领料单 以入库单中生产领料为准
     * @param $oneMenuName
     * @param $secMenuName
     * @return string
     */
    private function lingliaodan($oneMenuName, $secMenuName): string
    {
        //like查询数组
        $like = array(
            "orderNo" => $this->post('orderNo'),
        );
        $equal = [
            'coid' => self::$coid,
            'type' => '生产领料',
        ];
        $in = array(
            "state" => ['已出库', '已复核'],
        );
        $and = "";
        if ($this->post('createTime')) {
            $and .= self::timeComparison($this->post('createTime'), field: 'recordDate');
        }
        //获取查询结果
        $where = search([
                "like" => $like,
                '=' => $equal,
                'in' => $in
            ]) . $and;
        //排序
        $orderBy = $this->listSort(array(
            "search"  => $this->post('list_sort_key') ?: 'recordDate',
            "sort" => $this->post('list_sort') ?: "desc",
            "array"   => array(),
        ));
        $page = $this->page([
            "table" => 'inventory_changes',
            "where" => $where,
            "order" => $orderBy['orderBy']
        ]);
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = array(
            $this->checkAll("listForm", "listId")
        );
        //获取记录
        foreach ($page['array'] as $array) {
            //列表控件
            $dominate = array(
                "box" => array("name" => "listId", "value" => $array['id']),
            );
            $liButton[] = $this->dominate($dominate);
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    case 'type':
                        $row[] = '生产领料';
                        break;
                    case 'money':
                        $money = find(array('table' => 'inventory_changes_detail', 'single' => "orderId = '$array[id]'", 'field' => "ifnull(sum(money),0) as total"))['total'];
                        $row[] = self::lostZero($money);
                        break;
                    case 'stid':
                        $row[] = self::findStaff($array['stid']);
                        break;
                    case 'url':
                        $voucher = query('voucher', "id = '$array[voucherId]'");
                        $voucherWord = query('voucher_word', "id = '$voucher[voucherWordId]'");
                        $url = $array['voucherId'] ? (componentClass::urlTitle($voucherWord['word'] . '-' . $voucher['voucherNo'], root . 'control/adIndex.html?cla=voucherEdit&fun=room&id=' . $voucher['id'], '新增凭证')) : '';
                        $row[] = $url;
                        break;
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        $totalQuery = find([
            'table'  => 'inventory_changes_detail',
            'single' => " orderId in (select id from inventory_changes where 1=1 {$where}) ",
            'field'  => "ifnull(sum(money),0) as money",
        ]);
        $total    = [
            'money'        => self::lostZero($totalQuery['money']),
        ];
        //生成页面
        $li = $this->li(array(
            "body" => $body,
            "install" => $installSearch,
            "button" => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page" => $page['para'],
            "total"       => $total,
        ));
        //返回
        return $page['hint'] .
            $this->buttonGroup() .
            "<form name='listForm'>" .
                space .
                hidden(array('name' => 'typeOne', 'value' => $oneMenuName)) .
                hidden(array('name' => 'typeTwo', 'value' => $secMenuName)) .
                $li .
            "</form>" .
            $this->js() .
            $page['button'];
    }


    /**
     * 退料单
     * @param $oneMenuName
     * @param $secMenuName
     * @return string
     */
//    private function tuiliaodan($oneMenuName, $secMenuName)
//    {
//        //like查询数组
//        $like = array(
//            "orderNo" => $this->post('orderNo'),
//        );
//        $equal = [
//            "lineId" => $this->post('lineId'),
//            'coid' => self::$coid,
//            'state' => '已确认',
//        ];
//        $and = " and type = 1";
//        if ($this->post('createTime')) {
//            $and .= self::timeComparison($this->post('createTime'), 'date', 'recordDate');
//        }
//        //获取查询结果
//        $where = search([
//                "like" => $like,
//                '=' => $equal,
//            ]) . $and;
//        $page = $this->page([
//            "table" => 'pick_order',
//            "where" => $where,
//            "order" => "recordDate desc,orderNo desc",
//        ]);
//        //获取表头
//        $checkAll = $this->checkAll("listForm", "listId");
//        $tr[] = [$checkAll, '日期', '单据编号', '单据类型', '生产线', '单据金额', '制单人', '凭证字号'];
//        //获取记录
//        foreach ($page['array'] as $array) {
//            //列表控件
//            $dominate = array(
//                "box" => array("name" => "listId", "value" => $array['id']),
//            );
//            $money = find(array('table' => 'pick_order_detail', 'single' => "orderId = '$array[id]'", 'field' => "ifnull(sum(price*num),0) as total"))['total'];
//            $voucher = query('voucher', "id = '$array[voucherId]'");
//            $voucherWord = query('voucher_word', "id = '$voucher[voucherWordId]'");
//            $url = $array['voucherId'] ? (componentClass::urlTitle($voucherWord['word'] . '-' . $voucher['voucherNo'], root . 'control/adIndex.html?cla=voucherEdit&fun=room&id=' . $voucher['id'], '新增凭证')) : '';
//            //本列
//            $tr[] = [$this->dominate($dominate), $array['recordDate'], $array['orderNo'], '退料单', productLineClass::getName($array['lineId']), self::formattingFigure($money), self::findStaff($array['stid']), $url];
//        }
//        //返回
//        return $page['hint'] . $this->buttonGroup() . "<form name='listForm'>" . space . hidden(array('name' => 'typeOne', 'value' => $oneMenuName)) . hidden(array('name' => 'typeTwo', 'value' => $secMenuName)) . $this->tableMany(['tr' => $tr]) . "</form>" . $this->js() . $page['button'];
//    }


    /**
     * 退料单
     * @param $oneMenuName
     * @param $secMenuName
     * @return string
     */
    private function tuiliaodan($oneMenuName, $secMenuName)
    {
        //like查询数组
        $like = array(
            "orderNo" => $this->post('orderNo'),
        );
        $equal = [
            'coid' => self::$coid,
            'type' => '生产退料',
        ];
        $in = array(
            "state" => ['已入库', '已复核'],
        );
        $and = "";
        if ($this->post('createTime')) {
            $and .= self::timeComparison($this->post('createTime'), field: 'recordDate');
        }
        //获取查询结果
        $where = search([
                "like" => $like,
                '=' => $equal,
                'in' => $in
            ]) . $and;
        //排序
        $orderBy = $this->listSort(array(
            "search"  => $this->post('list_sort_key') ?: 'recordDate',
            "sort" => $this->post('list_sort') ?: "desc",
            "array"   => array(),
        ));
        $page = $this->page([
            "table" => 'inventory_changes',
            "where" => $where,
            "order" => $orderBy['orderBy']
        ]);
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = array(
            $this->checkAll("listForm", "listId")
        );
        //获取记录
        foreach ($page['array'] as $array) {
            //列表控件
            $dominate = array(
                "box" => array("name" => "listId", "value" => $array['id']),
            );
            $liButton[] = $this->dominate($dominate);
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    case 'type':
                        $row[] = '生产退料';
                        break;
                    case 'money':
                        $money = find(array('table' => 'inventory_changes_detail', 'single' => "orderId = '$array[id]'", 'field' => "ifnull(sum(money),0) as total"))['total'];
                        $row[] = self::lostZero($money);
                        break;
                    case 'stid':
                        $row[] = self::findStaff($array['stid']);
                        break;
                    case 'url':
                        $voucher = query('voucher', "id = '$array[voucherId]'");
                        $voucherWord = query('voucher_word', "id = '$voucher[voucherWordId]'");
                        $url = $array['voucherId'] ? (componentClass::urlTitle($voucherWord['word'] . '-' . $voucher['voucherNo'], root . 'control/adIndex.html?cla=voucherEdit&fun=room&id=' . $voucher['id'], '新增凭证')) : '';
                        $row[] = $url;
                        break;
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        $totalQuery = find([
            'table'  => 'inventory_changes_detail',
            'single' => " orderId in (select id from inventory_changes where 1=1 {$where}) ",
            'field'  => "ifnull(sum(money),0) as money",
        ]);
        $total    = [
            'money'        => self::lostZero($totalQuery['money']),
        ];
        //生成页面
        $li = $this->li(array(
            "body" => $body,
            "install" => $installSearch,
            "button" => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page" => $page['para'],
            "total"       => $total,
        ));
        //返回
        return $page['hint'] .
            $this->buttonGroup() .
            "<form name='listForm'>" .
                space .
                hidden(array('name' => 'typeOne', 'value' => $oneMenuName)) .
                hidden(array('name' => 'typeTwo', 'value' => $secMenuName)) .
                $li .
            "</form>" .
            $this->js() .
            $page['button'];
    }

    /**
     * 费用报销单
     */
    private function feiyongbaoxiaodan($oneMenuName, $secMenuName)
    {
        //like查询数组
        $like = array(
            "orderNo" => $this->post('orderNo'),
        );
        $equal = [
            'coid' => self::$coid,
            'state' => '已通过',
            'classify' => 2,
            'type' => $secMenuName
        ];
        $and = "";
        if ($this->post('createTime')) {
            $and .= self::timeComparison($this->post('createTime'), 'date', 'recordDate');
        }
        //获取查询结果
        $where = search([
                "like" => $like,
                '=' => $equal,
            ]) . $and;
        //排序
        $orderBy = $this->listSort(array(
            "search"  => $this->post('list_sort_key') ?: 'recordDate',
            "sort" => $this->post('list_sort') ?: "desc",
            "array"   => array(),
        ));
        $page = $this->page([
            "table" => 'cost',
            "where" => $where,
            "order" => $orderBy['orderBy']
        ]);
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = array(
            $this->checkAll("listForm", "listId")
        );
        //获取记录
        foreach ($page['array'] as $array) {
            //列表控件
            $dominate = array(
                "box" => array("name" => "listId", "value" => $array['id']),
            );
            $liButton[] = $this->dominate($dominate);
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    case 'type':
                        $row[] = $oneMenuName;
                        break;
                    case 'money':
                        $money = find(array('table' => 'cost_record', 'where' => "costId = '$array[id]'", 'sum' => "money"));
                        $row[] = self::lostZero($money);
                        break;
                    case 'stid':
                        $row[] = self::findStaff($array['stid']);
                        break;
                    case 'url':
                        $voucher = query('voucher', "id = '$array[voucherId]'");
                        $voucherWord = query('voucher_word', "id = '$voucher[voucherWordId]'");
                        $url = $array['voucherId'] ? (componentClass::urlTitle($voucherWord['word'] . '-' . $voucher['voucherNo'], root . 'control/adIndex.html?cla=voucherEdit&fun=room&id=' . $voucher['id'], '新增凭证')) : '';
                        $row[] = $url;
                        break;
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        $totalQuery = find([
            'table'  => 'cost_record',
            'single' => " costId in (select id from cost where 1=1 {$where}) ",
            'field'  => "ifnull(sum(money),0) as money",
        ]);
        $total    = [
            'money'        => self::lostZero($totalQuery['money']),
        ];
        //生成页面
        $li = $this->li(array(
            "body" => $body,
            "install" => $installSearch,
            "button" => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page" => $page['para'],
            "total"       => $total,
        ));
        //返回
        return $page['hint'] .
            $this->buttonGroup() .
            "<form name='listForm'>" .
                space .
                hidden(array('name' => 'typeOne', 'value' => $oneMenuName)) .
                hidden(array('name' => 'typeTwo', 'value' => $secMenuName)) .
                $li .
            "</form>" .
            $this->js() .
            $page['button'];
    }

    /**
     * 转账单
     */
    private function zhuanzhangdan($oneMenuName, $secMenuName)
    {
        //like查询数组
        $like = array(
            "orderNo" => $this->post('orderNo'),
        );
        $equal = [
            'coid' => self::$coid,
            'state' => '已确认',
        ];
        $and = "";
        if ($this->post('createTime')) {
            $and .= self::timeComparison($this->post('createTime'), 'date', 'recordDate');
        }
        //获取查询结果
        $where = search([
                "like" => $like,
                '=' => $equal,
            ]) . $and;
        //排序
        $orderBy = $this->listSort(array(
            "search"  => $this->post('list_sort_key') ?: 'recordDate',
            "sort" => $this->post('list_sort') ?: "desc",
            "array"   => array(),
        ));
        $page = $this->page([
            "table" => 'transfer_order',
            "where" => $where,
            "order" => $orderBy['orderBy']
        ]);
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = array(
            $this->checkAll("listForm", "listId")
        );
        //获取记录
        foreach ($page['array'] as $array) {
            //列表控件
            $dominate = array(
                "box" => array("name" => "listId", "value" => $array['id']),
            );
            $liButton[] = $this->dominate($dominate);
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    case 'type':
                        $row[] = $oneMenuName;
                        break;
                    case 'money':
                        $row[] = self::lostZero($array['money']);
                        break;
                    case 'stid':
                        $row[] = self::findStaff($array['stid']);
                        break;
                    case 'url':
                        $voucher = query('voucher', "id = '$array[voucherId]'");
                        $voucherWord = query('voucher_word', "id = '$voucher[voucherWordId]'");
                        $url = $array['voucherId'] ? (componentClass::urlTitle($voucherWord['word'] . '-' . $voucher['voucherNo'], root . 'control/adIndex.html?cla=voucherEdit&fun=room&id=' . $voucher['id'], '新增凭证')) : '';
                        $row[] = $url;
                        break;
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        $totalQuery = find([
            'table'  => 'transfer_order',
            'single' => $where,
            'field'  => "ifnull(sum(money),0) as money",
        ]);
        $total    = [
            'money'        => self::lostZero($totalQuery['money']),
        ];
        //生成页面
        $li = $this->li(array(
            "body" => $body,
            "install" => $installSearch,
            "button" => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page" => $page['para'],
            "total"       => $total,
        ));
        //返回
        return $page['hint'] .
            $this->buttonGroup() .
            "<form name='listForm'>" .
                space .
                hidden(array('name' => 'typeOne', 'value' => $oneMenuName)) .
                hidden(array('name' => 'typeTwo', 'value' => $secMenuName)) .
                $li .
            "</form>" .
            $this->js() .
            $page['button'];
    }

    /**
     *核销单
     * @param $oneMenuName
     * @param $secMenuName
     * @return string
     */
    private function hexiaodan($oneMenuName, $secMenuName): string
    {
        //like查询数组
        $like = array(
            "orderNo" => $this->post('orderNo'),
        );
        $equal = [
            'coid' => self::$coid,
            'state' => '已确认',
            'type' => $secMenuName
        ];
        $and = "";
        if ($this->post('createTime')) {
            $and .= self::timeComparison($this->post('createTime'), 'date', 'recordDate');
        }
        if ($this->post('khid')) {
            $and .= " and targetId = '" . $this->post('khid') . "'";
        }
        if ($this->post('supplierId')) {
            $and .= " and targetId = '" . $this->post('supplierId') . "'";
        }
        //获取查询结果
        $where = search([
                "like" => $like,
                '=' => $equal,
            ]) . $and;
        //排序
        $orderBy = $this->listSort(array(
            "search"  => $this->post('list_sort_key') ?: 'recordDate',
            "sort" => $this->post('list_sort') ?: "desc",
            "array"   => array(),
        ));
        $page = $this->page([
            "table" => 'write_off_order',
            "where" => $where,
            "order" => $orderBy['orderBy']
        ]);
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = array(
            $this->checkAll("listForm", "listId")
        );
        //获取记录
        foreach ($page['array'] as $array) {
            //列表控件
            $dominate = array(
                "box" => array("name" => "listId", "value" => $array['id']),
            );
            $liButton[] = $this->dominate($dominate);
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    case 'type':
                        $row[] = $oneMenuName;
                        break;
                    case 'companyName':
                        $kehu = query('kehu', "khid = '$array[targetId]'");
                        $companyName = $kehu['companyName'] ?: query('supplier', "id = '$array[targetId]'")['name'];
                        $row[] = $companyName;
                        break;
                    case 'money':
                        $money = find(array('table' => 'write_off_order_detail', 'where' => "orderId = '$array[id]'", 'sum' => "money"));
                        $row[] = $money;
                        break;
                    case 'stid':
                        $row[] = self::findStaff($array['stid']);
                        break;
                    case 'url':
                        $voucher = query('voucher', "id = '$array[voucherId]'");
                        $voucherWord = query('voucher_word', "id = '$voucher[voucherWordId]'");
                        $url = $array['voucherId'] ? (componentClass::urlTitle($voucherWord['word'] . '-' . $voucher['voucherNo'], root . 'control/adIndex.html?cla=voucherEdit&fun=room&id=' . $voucher['id'], '新增凭证')) : '';
                        $row[] = $url;
                        break;
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        $totalQuery = find([
            'table'  => "write_off_order_detail",
            'single' => " orderId in (select id from write_off_order where 1=1 {$where}) ",
            'field'  => "ifnull(sum(money),0) as money",
        ]);
        $total    = [
            'money'        => self::lostZero($totalQuery['money']),
        ];
        //生成页面
        $li = $this->li(array(
            "body" => $body,
            "install" => $installSearch,
            "button" => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page" => $page['para'],
            "total"       => $total,
        ));
        //返回
        return $page['hint'] .
            $this->buttonGroup() .
            "<form name='listForm'>" .
            space .
            hidden(array('name' => 'typeOne', 'value' => $oneMenuName)) .
            hidden(array('name' => 'typeTwo', 'value' => $secMenuName)) .
            $li .
            "</form>" .
            $this->js() .
            $page['button'];
    }

    /**
     * 结转出库成本
     * @param $oneMenuName
     * @param $secMenuName
     * @return string
     */
    private function jiezhuanchukuchengben($oneMenuName, $secMenuName): string
    {
        //like查询数组
        $like = array(
            "orderNo" => $this->post('orderNo'),
        );
        $and = '';
        $equal = [];
        $sourceOrder = '';
        if ($secMenuName == '出库') {
            $equal = [
                "targetId" => $this->post('khid'),
                'coid' => self::$coid,
                'type' => '销售发货'
            ];
            $and = " and classify = 0 and state in ('已出库','已复核')";
            $sourceOrder = '销售发货单';
        } elseif ($secMenuName == '退货') {
            $equal = [
                "targetId" => $this->post('khid'),
                'coid' => self::$coid,
                'type' => '销售退货',
                'classify' => 1,
            ];
            $sourceOrder = '销售退货单';
            $and = " and state in ('已入库','已复核')";
        }


        if ($this->post('createTime')) {
            $and .= self::timeComparison($this->post('createTime'));
        }
        //获取查询结果
        $where = search([
                "like" => $like,
                '=' => $equal,
            ]) . $and;
        //排序
        $orderBy = $this->listSort(array(
            "search"  => $this->post('list_sort_key') ?: 'recordDate',
            "sort" => $this->post('list_sort') ?: "desc",
            "array"   => array(),
        ));
        $page = $this->page([
            "table" => 'inventory_changes',
            "where" => $where,
            "order" => $orderBy['orderBy']
        ]);
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = array(
            $this->checkAll("listForm", "listId")
        );
        //获取记录
        foreach ($page['array'] as $array) {
            //列表控件
            $dominate = array(
                "box" => array("name" => "listId", "value" => $array['id']),
            );
            $liButton[] = $this->dominate($dominate);
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    case 'type':
                        $row[] = $sourceOrder;
                        break;
                    case 'companyName':
                        $kehu = query('kehu', "khid = '$array[targetId]'");
                        $row[] = $kehu['companyName'];
                        break;
                    case 'money':
                        $money = find(array('table' => 'inventory_changes_detail', 'single' => "orderId = '$array[id]'", 'field' => "ifnull(sum(money),0) as total"))['total'];
                        $row[] = $money;
                        break;
                    case 'stid':
                        $row[] = self::findStaff($array['stid']);
                        break;
                    case 'url':
                        $voucher = query('voucher', "id = '$array[voucherId]'");
                        $voucherWord = query('voucher_word', "id = '$voucher[voucherWordId]'");
                        $url = $array['voucherId'] ? (componentClass::urlTitle($voucherWord['word'] . '-' . $voucher['voucherNo'], root . 'control/adIndex.html?cla=voucherEdit&fun=room&id=' . $voucher['id'], '新增凭证')) : '';
                        $row[] = $url;
                        break;
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        $totalQuery = find([
            'table'  => "inventory_changes_detail",
            'single' => " orderId in (select id from inventory_changes where 1=1 {$where}) ",
            'field'  => "ifnull(sum(money),0) as money",
        ]);
        $total    = [
            'money'        => self::lostZero($totalQuery['money']),
        ];
        //生成页面
        $li = $this->li(array(
            "body" => $body,
            "install" => $installSearch,
            "button" => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page" => $page['para'],
            "total"       => $total,
        ));
        //返回
        return $page['hint'] .
            $this->buttonGroup() .
            "<form name='listForm'>" .
                space .
                hidden(array('name' => 'typeOne', 'value' => $oneMenuName)) .
                hidden(array('name' => 'typeTwo', 'value' => $secMenuName)) .
                $li .
            "</form>" .
            $this->js() .
            $page['button'];
    }

    /**
     *组装单
     * @param $oneMenuName
     * @param $secMenuName
     * @return string
     */
    private function zuzhuangdan($oneMenuName, $secMenuName): string
    {
        //like查询数组
        $like = array(
            "orderNo" => $this->post('orderNo'),
        );
        $equal = [
            'coid' => self::$coid,
            'state' => '已确认',
        ];
        $and = "";
        if ($this->post('createTime')) {
            $and .= self::timeComparison($this->post('createTime'), 'date', 'orderDate');
        }
        //获取查询结果
        $where = search([
                "like" => $like,
                '=' => $equal,
            ]) . $and;
        //排序
        $orderBy = $this->listSort(array(
            "search"  => $this->post('list_sort_key') ?: 'orderDate',
            "sort" => $this->post('list_sort') ?: "desc",
            "array"   => array(),
        ));
        $page = $this->page([
            "table" => 'inventory_assembly',
            "where" => $where,
            "order" => $orderBy['orderBy']
        ]);
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = array(
            $this->checkAll("listForm", "listId")
        );
        //获取记录
        foreach ($page['array'] as $array) {
            //列表控件
            $dominate = array(
                "box" => array("name" => "listId", "value" => $array['id']),
            );
            $liButton[] = $this->dominate($dominate);
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    case 'type':
                        $row[] = $oneMenuName;
                        break;
                    case 'money':
                        $money = find(array('table' => 'inventory_assembly_detail', 'single' => "orderId = '$array[id]'", 'field' => "ifnull(sum(num*price),0) as total"))['total'];
                        $row[] = $money;
                        break;
                    case 'stid':
                        $row[] = self::findStaff($array['stid']);
                        break;
                    case 'url':
                        $voucher = query('voucher', "id = '$array[voucherId]'");
                        $voucherWord = query('voucher_word', "id = '$voucher[voucherWordId]'");
                        $url = $array['voucherId'] ? (componentClass::urlTitle($voucherWord['word'] . '-' . $voucher['voucherNo'], root . 'control/adIndex.html?cla=voucherEdit&fun=room&id=' . $voucher['id'], '新增凭证')) : '';
                        $row[] = $url;
                        break;
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        $totalQuery = find([
            'table'  => "inventory_assembly_detail",
            'single' => " orderId in (select id from inventory_assembly where 1=1 {$where}) ",
            'field'  => "ifnull(sum(num*price),0) as money",
        ]);
        $total    = [
            'money'        => self::lostZero($totalQuery['money']),
        ];
        //生成页面
        $li = $this->li(array(
            "body" => $body,
            "install" => $installSearch,
            "button" => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page" => $page['para'],
            "total"       => $total,
        ));
        //返回
        return $page['hint'] .
            $this->buttonGroup() .
            "<form name='listForm'>" .
                space .
                hidden(array('name' => 'typeOne', 'value' => $oneMenuName)) .
                hidden(array('name' => 'typeTwo', 'value' => $secMenuName)) .
                $li .
            "</form>" .
            $this->js() .
            $page['button'];
    }


    /**
     *拆卸单
     * @param $oneMenuName
     * @param $secMenuName
     * @return string
     */
    private function chaixiedan($oneMenuName, $secMenuName): string
    {
        //like查询数组
        $like = array(
            "orderNo" => $this->post('orderNo'),
        );
        $equal = [
            'coid' => self::$coid,
            'state' => '已确认',
        ];
        $and = "";
        if ($this->post('createTime')) {
            $and .= self::timeComparison($this->post('createTime'), 'date', 'orderDate');
        }
        //获取查询结果
        $where = search([
                "like" => $like,
                '=' => $equal,
            ]) . $and;
        //排序
        $orderBy = $this->listSort(array(
            "search"  => $this->post('list_sort_key') ?: 'orderDate',
            "sort" => $this->post('list_sort') ?: "desc",
            "array"   => array(),
        ));
        $page = $this->page([
            "table" => 'inventory_dismantle',
            "where" => $where,
            "order" => $orderBy['orderBy']
        ]);
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = array(
            $this->checkAll("listForm", "listId")
        );
        //获取记录
        foreach ($page['array'] as $array) {
            //列表控件
            $dominate = array(
                "box" => array("name" => "listId", "value" => $array['id']),
            );
            $liButton[] = $this->dominate($dominate);
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    case 'type':
                        $row[] = $oneMenuName;
                        break;
                    case 'money':
                        $money = find(array('table' => 'inventory_dismantle_detail', 'single' => "orderId = '$array[id]'", 'field' => "ifnull(sum(price*num),0) as total"))['total'];
                        $row[] = $money;
                        break;
                    case 'stid':
                        $row[] = self::findStaff($array['stid']);
                        break;
                    case 'url':
                        $voucher = query('voucher', "id = '$array[voucherId]'");
                        $voucherWord = query('voucher_word', "id = '$voucher[voucherWordId]'");
                        $url = $array['voucherId'] ? (componentClass::urlTitle($voucherWord['word'] . '-' . $voucher['voucherNo'], root . 'control/adIndex.html?cla=voucherEdit&fun=room&id=' . $voucher['id'], '新增凭证')) : '';
                        $row[] = $url;
                        break;
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        $totalQuery = find([
            'table'  => "inventory_dismantle_detail",
            'single' => " orderId in (select id from inventory_dismantle where 1=1 {$where}) ",
            'field'  => "ifnull(sum(num*price),0) as money",
        ]);
        $total    = [
            'money'        => self::lostZero($totalQuery['money']),
        ];
        //生成页面
        $li = $this->li(array(
            "body" => $body,
            "install" => $installSearch,
            "button" => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page" => $page['para'],
            "total"       => $total,
        ));
        //返回
        return $page['hint'] .
            $this->buttonGroup() .
            "<form name='listForm'>" .
            space .
            hidden(array('name' => 'typeOne', 'value' => $oneMenuName)) .
            hidden(array('name' => 'typeTwo', 'value' => $secMenuName)) .
            $li .
            "</form>" .
            $this->js() .
            $page['button'];
    }

    /**
     *成本调整单
     * @param $oneMenuName
     * @param $secMenuName
     * @return string
     */
    private function chengbendiaozhengdan($oneMenuName, $secMenuName): string
    {
        //like查询数组
        $like = array(
            "orderNo" => $this->post('orderNo'),
        );
        $equal = [
            'coid' => self::$coid,
            'state' => '已确认',
        ];
        $and = "";
        if ($this->post('createTime')) {
            $and .= self::timeComparison($this->post('createTime'), 'date', 'orderDate');
        }
        //获取查询结果
        $where = search([
                "like" => $like,
                '=' => $equal,
            ]) . $and;
        //排序
        $orderBy = $this->listSort(array(
            "search"  => $this->post('list_sort_key') ?: 'orderDate',
            "sort" => $this->post('list_sort') ?: "desc",
            "array"   => array(),
        ));
        $page = $this->page([
            "table" => 'inventory_cost',
            "where" => $where,
            "order" => $orderBy['orderBy']
        ]);
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = array(
            $this->checkAll("listForm", "listId")
        );
        //获取记录
        foreach ($page['array'] as $array) {
            //列表控件
            $dominate = array(
                "box" => array("name" => "listId", "value" => $array['id']),
            );
            $liButton[] = $this->dominate($dominate);
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    case 'type':
                        $row[] = '成本调整单';
                        break;
                    case 'companyName':
                        $row[] = '';
                        break;
                    case 'money':
                        $money = find(array('table' => 'inventory_cost_detail', 'where' => "orderId = '$array[id]'", 'sum' => "price"));
                        $row[] = $money;
                        break;
                    case 'stid':
                        $row[] = self::findStaff($array['stid']);
                        break;
                    case 'url':
                        $voucher = query('voucher', "id = '$array[voucherId]'");
                        $voucherWord = query('voucher_word', "id = '$voucher[voucherWordId]'");
                        $url = $array['voucherId'] ? (componentClass::urlTitle($voucherWord['word'] . '-' . $voucher['voucherNo'], root . 'control/adIndex.html?cla=voucherEdit&fun=room&id=' . $voucher['id'], '新增凭证')) : '';
                        $row[] = $url;
                        break;
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        $totalQuery = find([
            'table'  => "inventory_cost_detail",
            'single' => " orderId in (select id from inventory_cost where 1=1 {$where}) ",
            'field'  => "ifnull(sum(price),0) as money",
        ]);
        $total    = [
            'money'        => self::lostZero($totalQuery['money']),
        ];
        //生成页面
        $li = $this->li(array(
            "body" => $body,
            "install" => $installSearch,
            "button" => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page" => $page['para'],
            "total"       => $total,
        ));
        //返回
        return $page['hint'] .
            $this->buttonGroup() .
            "<form name='listForm'>" .
                space .
                hidden(array('name' => 'typeOne', 'value' => $oneMenuName)) .
                hidden(array('name' => 'typeTwo', 'value' => $secMenuName)) .
                $li .
            "</form>" .
            $this->js() .
            $page['button'];
    }

    /**
     * 生产入库单
     * @param $oneMenuName
     * @param $secMenuName
     * @return string
     */
    private function shengchanrukudan($oneMenuName, $secMenuName): string
    {
        //like查询数组
        $like = array(
            "orderNo" => $this->post('orderNo'),
        );
        $equal = [
            'coid' => self::$coid,
            'type' => '生产入库',
        ];
        $in = array(
            "state" => ['已入库', '已复核'],
        );
        $and = " and classify = 1";
        if ($this->post('createTime')) {
            $and .= self::timeComparison($this->post('createTime'), field: 'recordDate');
        }

        //获取查询结果
        $where = search([
                "like" => $like,
                '=' => $equal,
                'in' => $in
            ]) . $and;
        //排序
        $orderBy = $this->listSort(array(
            "search"  => $this->post('list_sort_key') ?: 'recordDate',
            "sort" => $this->post('list_sort') ?: "desc",
            "array"   => array(),
        ));
        $page = $this->page([
            "table" => 'inventory_changes',
            "where" => $where,
            "order" => $orderBy['orderBy']
        ]);
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = array(
            $this->checkAll("listForm", "listId")
        );
        //获取记录
        foreach ($page['array'] as $array) {
            //列表控件
            $dominate = array(
                "box" => array("name" => "listId", "value" => $array['id']),
            );
            $liButton[] = $this->dominate($dominate);
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    case 'type':
                        $row[] = '生产入库单';
                        break;
                    case 'money':
                        $money = find(array('table' => 'inventory_changes_detail', 'single' => "orderId = '$array[id]'", 'field' => "ifnull(sum(money),0) as total"))['total'];
                        $row[] = self::lostZero($money);
                        break;
                    case 'stid':
                        $row[] = self::findStaff($array['stid']);
                        break;
                    case 'url':
                        $voucher = query('voucher', "id = '$array[voucherId]'");
                        $voucherWord = query('voucher_word', "id = '$voucher[voucherWordId]'");
                        $url = $array['voucherId'] ? (componentClass::urlTitle($voucherWord['word'] . '-' . $voucher['voucherNo'], root . 'control/adIndex.html?cla=voucherEdit&fun=room&id=' . $voucher['id'], '新增凭证')) : '';
                        $row[] = $url;
                        break;
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        $totalQuery = find([
            'table'  => "inventory_changes_detail",
            'single' => " orderId in (select id from inventory_changes where 1=1 {$where}) ",
            'field'  => "ifnull(sum(money),0) as money",
        ]);
        $total    = [
            'money'        => self::lostZero($totalQuery['money']),
        ];
        //生成页面
        $li = $this->li(array(
            "body" => $body,
            "install" => $installSearch,
            "button" => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page" => $page['para'],
            "total"       => $total,
        ));
        //返回
        return $page['hint'] .
            $this->buttonGroup() .
            "<form name='listForm'>" .
                space .
                hidden(array('name' => 'typeOne', 'value' => $oneMenuName)) .
                hidden(array('name' => 'typeTwo', 'value' => $secMenuName)) .
                $li .
            "</form>" .
            $this->js() .
            $page['button'];
    }


    /**
     *其他出库单
     * @param $oneMenuName
     * @param $secMenuName
     * @return string
     */
    private function qitachukudan($oneMenuName, $secMenuName): string
    {
        //like查询数组
        $like = array(
            "orderNo" => $this->post('orderNo'),
        );
        $equal = [
            'coid' => self::$coid,
            'type' => '其他出库',
//            'subCategory' => $secMenuName
        ];
        $in = array(
            "state" => ['已出库', '已复核'],
        );
        $and = " and classify = 0";
        if ($this->post('createTime')) {
            $and .= self::timeComparison($this->post('createTime'), field: 'recordDate');
        }
        if ($this->post('khid')) {
            $and .= " and targetId = '" . $this->post('khid') . "'";
        }
        if ($this->post('supplierId')) {
            $and .= " and targetId = '" . $this->post('supplierId') . "'";
        }
        //获取查询结果
        $where = search([
                "like" => $like,
                '=' => $equal,
                'in' => $in
            ]) . $and;
        //排序
        $orderBy = $this->listSort(array(
            "search"  => $this->post('list_sort_key') ?: 'recordDate',
            "sort" => $this->post('list_sort') ?: "desc",
            "array"   => array(),
        ));
        $page = $this->page([
            "table" => 'inventory_changes',
            "where" => $where,
            "order" => $orderBy['orderBy']
        ]);
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = array(
            $this->checkAll("listForm", "listId")
        );
        //获取记录
        foreach ($page['array'] as $array) {
            //列表控件
            $dominate = array(
                "box" => array("name" => "listId", "value" => $array['id']),
            );
            $liButton[] = $this->dominate($dominate);
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    case 'type':
                        $row[] = '其他出库单';
                        break;
                    case 'companyName':
                        $kehu = query('kehu', "khid = '$array[targetId]'");
                        $companyName = $kehu['companyName'] ?: query('supplier', "id = '$array[targetId]'")['name'];
                        $row[] = $companyName;
                        break;
                    case 'money':
                        $money = find(array('table' => 'inventory_changes_detail', 'single' => "orderId = '$array[id]'", 'field' => "ifnull(sum(money),0) as total"))['total'];
                        $row[] = self::lostZero($money);
                        break;
                    case 'stid':
                        $row[] = self::findStaff($array['stid']);
                        break;
                    case 'url':
                        $voucher = query('voucher', "id = '$array[voucherId]'");
                        $voucherWord = query('voucher_word', "id = '$voucher[voucherWordId]'");
                        $url = $array['voucherId'] ? (componentClass::urlTitle($voucherWord['word'] . '-' . $voucher['voucherNo'], root . 'control/adIndex.html?cla=voucherEdit&fun=room&id=' . $voucher['id'], '新增凭证')) : '';
                        $row[] = $url;
                        break;
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        $totalQuery = find([
            'table'  => "inventory_changes_detail",
            'single' => " orderId in (select id from inventory_changes where 1=1 {$where}) ",
            'field'  => "ifnull(sum(money),0) as money",
        ]);
        $total    = [
            'money'        => self::lostZero($totalQuery['money']),
        ];
        //生成页面
        $li = $this->li(array(
            "body" => $body,
            "install" => $installSearch,
            "button" => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page" => $page['para'],
            "total"       => $total,
        ));
        //返回
        return $page['hint'] .
            $this->buttonGroup() .
            "<form name='listForm'>" .
                space .
                hidden(array('name' => 'typeOne', 'value' => $oneMenuName)) .
                hidden(array('name' => 'typeTwo', 'value' => $secMenuName)) .
                $li .
            "</form>" .
            $this->js() .
            $page['button'];
    }

    /**
     *  销项发票
     * @param $oneMenuName
     * @param $secMenuName
     * @return string
     */
    private function xiaoxiangfapiao($oneMenuName, $secMenuName)
    {
        //like查询数组
        $like = array(
            "invoiceNo" => $this->post('orderNo'),
        );
        $equal = [
            'coid' => self::$coid,
            'type' => '开票',
        ];
        $and = "";
        if ($this->post('createTime')) {
            $and .= self::timeComparison($this->post('createTime'), field: 'invoiceDate');
        }
        if ($this->post('khid')) {
            $and .= " and khid = '" . $this->post('khid') . "'";
        }
        if ($this->post('supplierId')) {
            $and .= " and khid = '" . $this->post('supplierId') . "'";
        }
        //获取查询结果
        $where = search([
                "like" => $like,
                '=' => $equal,
            ]) . $and;
        //排序
        $orderBy = $this->listSort(array(
            "search"  => $this->post('list_sort_key') ?: 'invoiceDate',
            "sort" => $this->post('list_sort') ?: "desc",
            "array"   => array(),
        ));
        $page = $this->page([
            "table" => 'invoice_open',
            "where" => $where,
            "order" => $orderBy['orderBy']
        ]);
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = array(
            $this->checkAll("listForm", "listId")
        );
        //获取记录
        foreach ($page['array'] as $array) {
            //列表控件
            $dominate = array(
                "box" => array("name" => "listId", "value" => $array['id']),
            );
            $liButton[] = $this->dominate($dominate);
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    case 'type':
                        $row[] = $oneMenuName;
                        break;
                    case 'companyName':
                        $row[] = $array['buyCompany'];
                        break;
                    case 'money':
                        $row[] = self::lostZero($array['money']);
                        break;
                    case 'unTaxMoney':
                        $row[] = self::lostZero($array['unTaxMoney']);
                        break;
                    case 'taxMoney':
                        $row[] = self::lostZero($array['taxMoney']);
                        break;
                    case 'url':
                        $voucher = query('voucher', "id = '$array[voucherId]'");
                        $voucherWord = query('voucher_word', "id = '$voucher[voucherWordId]'");
                        $url = $array['voucherId'] ? (componentClass::urlTitle($voucherWord['word'] . '-' . $voucher['voucherNo'], root . 'control/adIndex.html?cla=voucherEdit&fun=room&id=' . $voucher['id'], '新增凭证')) : '';
                        $row[] = $url;
                        break;
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        $totalQuery = find([
            'table'  => "invoice_open",
            'single' => $where,
            'field'  => "ifnull(sum(money),0) as money,ifnull(sum(unTaxMoney),0) as unTaxMoney,ifnull(sum(taxMoney),0) as taxMoney",
        ]);
        $total    = [
            'money'        => self::lostZero($totalQuery['money']),
            'unTaxMoney'        => self::lostZero($totalQuery['unTaxMoney']),
            'taxMoney'        => self::lostZero($totalQuery['taxMoney']),
        ];
        //生成页面
        $li = $this->li(array(
            "body" => $body,
            "install" => $installSearch,
            "button" => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page" => $page['para'],
            "total"       => $total,
        ));
        //返回
        return $page['hint'] .
            $this->buttonGroup() .
            "<form name='listForm'>" .
                space .
                hidden(array('name' => 'typeOne', 'value' => $oneMenuName)) .
                hidden(array('name' => 'typeTwo', 'value' => $secMenuName)) .
                $li .
            "</form>" .
            $this->js() .
            $page['button'];
    }

    /**
     *  进项发票
     * @param $oneMenuName
     * @param $secMenuName
     * @return string
     */
    private function jinxiangfapiao($oneMenuName, $secMenuName)
    {
        //like查询数组
        $like = array(
            "invoiceNo" => $this->post('orderNo'),
        );
        $equal = [
            'coid' => self::$coid,
            'type' => '收票',
        ];
        $and = "";
        if ($this->post('createTime')) {
            $and .= self::timeComparison($this->post('createTime'), field: 'invoiceDate');
        }
        if ($this->post('khid')) {
            $and .= " and khid = '" . $this->post('khid') . "'";
        }
        if ($this->post('supplierId')) {
            $and .= " and khid = '" . $this->post('supplierId') . "'";
        }
        //获取查询结果
        $where = search([
                "like" => $like,
                '=' => $equal,
            ]) . $and;
        //排序
        $orderBy = $this->listSort(array(
            "search"  => $this->post('list_sort_key') ?: 'invoiceDate',
            "sort" => $this->post('list_sort') ?: "desc",
            "array"   => array(),
        ));
        $page = $this->page([
            "table" => 'invoice_open',
            "where" => $where,
            "order" => $orderBy['orderBy']
        ]);
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = array(
            $this->checkAll("listForm", "listId")
        );
        //获取记录
        foreach ($page['array'] as $array) {
            //列表控件
            $dominate = array(
                "box" => array("name" => "listId", "value" => $array['id']),
            );
            $liButton[] = $this->dominate($dominate);
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    case 'type':
                        $row[] = $oneMenuName;
                        break;
                    case 'companyName':
                        $row[] = $array['saleCompany'];
                        break;
                    case 'money':
                        $row[] = self::lostZero($array['money']);
                        break;
                    case 'unTaxMoney':
                        $row[] = self::lostZero($array['unTaxMoney']);
                        break;
                    case 'taxMoney':
                        $row[] = self::lostZero($array['taxMoney']);
                        break;
                    case 'url':
                        $voucher = query('voucher', "id = '$array[voucherId]'");
                        $voucherWord = query('voucher_word', "id = '$voucher[voucherWordId]'");
                        $url = $array['voucherId'] ? (componentClass::urlTitle($voucherWord['word'] . '-' . $voucher['voucherNo'], root . 'control/adIndex.html?cla=voucherEdit&fun=room&id=' . $voucher['id'], '新增凭证')) : '';
                        $row[] = $url;
                        break;
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        $totalQuery = find([
            'table'  => "invoice_open",
            'single' => $where,
            'field'  => "ifnull(sum(money),0) as money,ifnull(sum(unTaxMoney),0) as unTaxMoney,ifnull(sum(taxMoney),0) as taxMoney",
        ]);
        $total    = [
            'money'        => self::lostZero($totalQuery['money']),
            'unTaxMoney'        => self::lostZero($totalQuery['unTaxMoney']),
            'taxMoney'        => self::lostZero($totalQuery['taxMoney']),
        ];
        //生成页面
        $li = $this->li(array(
            "body" => $body,
            "install" => $installSearch,
            "button" => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page" => $page['para'],
            "total"       => $total,
        ));
        //返回
        return $page['hint'] .
            $this->buttonGroup() .
            "<form name='listForm'>" .
            space .
            hidden(array('name' => 'typeOne', 'value' => $oneMenuName)) .
            hidden(array('name' => 'typeTwo', 'value' => $secMenuName)) .
            $li .
            "</form>" .
            $this->js() .
            $page['button'];
    }

    /**
     *其他入库单
     * @param $oneMenuName
     * @param $secMenuName
     * @return string
     */
    private function qitarukudan($oneMenuName, $secMenuName): string
    {
        //like查询数组
        $like = array(
            "orderNo" => $this->post('orderNo'),
        );
        $equal = [
            'coid' => self::$coid,
            'type' => '其他入库',
            'classify' => 1,
//            'subCategory' => $secMenuName
        ];
        $in = array(
            "state" => ['已入库', '已复核'],
        );
        $and = "";
        if ($this->post('createTime')) {
            $and .= self::timeComparison($this->post('createTime'), field: 'recordDate');
        }
        if ($this->post('khid')) {
            $and .= " and targetId = '" . $this->post('khid') . "'";
        }
        if ($this->post('supplierId')) {
            $and .= " and targetId = '" . $this->post('supplierId') . "'";
        }
        //获取查询结果
        $where = search([
                "like" => $like,
                '=' => $equal,
                'in' => $in
            ]) . $and;
        //排序
        $orderBy = $this->listSort(array(
            "search"  => $this->post('list_sort_key') ?: 'recordDate',
            "sort" => $this->post('list_sort') ?: "desc",
            "array"   => array(),
        ));
        $page = $this->page([
            "table" => 'inventory_changes',
            "where" => $where,
            "order" => $orderBy['orderBy']
        ]);
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = array(
            $this->checkAll("listForm", "listId")
        );
        //获取记录
        foreach ($page['array'] as $array) {
            //列表控件
            $dominate = array(
                "box" => array("name" => "listId", "value" => $array['id']),
            );
            $liButton[] = $this->dominate($dominate);
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    case 'type':
                        $row[] = '其他入库单';
                        break;
                    case 'companyName':
                        $kehu = query('kehu', "khid = '$array[targetId]'");
                        $companyName = $kehu['companyName'] ?: query('supplier', "id = '$array[targetId]'")['name'];
                        $row[] = $companyName;
                        break;
                    case 'money':
                        $money = find(array('table' => 'inventory_changes_detail', 'single' => "orderId = '$array[id]'", 'field' => "ifnull(sum(money),0) as total"))['total'];
                        $row[] = self::lostZero($money);
                        break;
                    case 'stid':
                        $row[] = self::findStaff($array['stid']);
                        break;
                    case 'url':
                        $voucher = query('voucher', "id = '$array[voucherId]'");
                        $voucherWord = query('voucher_word', "id = '$voucher[voucherWordId]'");
                        $url = $array['voucherId'] ? (componentClass::urlTitle($voucherWord['word'] . '-' . $voucher['voucherNo'], root . 'control/adIndex.html?cla=voucherEdit&fun=room&id=' . $voucher['id'], '新增凭证')) : '';
                        $row[] = $url;
                        break;
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        $totalQuery = find([
            'table'  => "inventory_changes_detail",
            'single' => " orderId in (select id from inventory_changes where 1=1 {$where}) ",
            'field'  => "ifnull(sum(money),0) as money",
        ]);
        $total    = [
            'money'        => self::lostZero($totalQuery['money']),
        ];
        //生成页面
        $li = $this->li(array(
            "body" => $body,
            "install" => $installSearch,
            "button" => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page" => $page['para'],
            "total"       => $total,
        ));
        //返回
        return $page['hint'] .
            $this->buttonGroup() .
            "<form name='listForm'>" .
                space .
                hidden(array('name' => 'typeOne', 'value' => $oneMenuName)) .
                hidden(array('name' => 'typeTwo', 'value' => $secMenuName)) .
                $li .
            "</form>" .
            $this->js() .
            $page['button'];
    }

    /**
     *收入单
     * @param $oneMenuName
     * @param $secMenuName
     * @return string
     */
    private function shourudan($oneMenuName, $secMenuName): string
    {
        //like查询数组
        $like = array(
            "orderNo" => $this->post('orderNo'),
        );
        $equal = [
            'coid' => self::$coid,
            'state' => '已确认',
            'target' => $this->post('type')
        ];
        $and = " and classify = 2";
        if ($this->post('createTime')) {
            $and .= self::timeComparison($this->post('createTime'), 'date', 'recordDate');
        }
        if ($this->post('khid')) {
            $and .= " and targetId = '" . $this->post('khid') . "'";
        }
        if ($this->post('supplierId')) {
            $and .= " and targetId = '" . $this->post('supplierId') . "'";
        }
        //获取查询结果
        $where = search([
                "like" => $like,
                '=' => $equal,
            ]) . $and;
        //排序
        $orderBy = $this->listSort(array(
            "search"  => $this->post('list_sort_key') ?: 'recordDate',
            "sort" => $this->post('list_sort') ?: "desc",
            "array"   => array(),
        ));
        $page = $this->page([
            "table" => 'other_receipt',
            "where" => $where,
            "order" => $orderBy['orderBy']
        ]);
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = array(
            $this->checkAll("listForm", "listId")
        );
        //获取记录
        foreach ($page['array'] as $array) {
            //列表控件
            $dominate = array(
                "box" => array("name" => "listId", "value" => $array['id']),
            );
            $liButton[] = $this->dominate($dominate);
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    case 'type':
                        $row[] = '收入单';
                        break;
                    case 'companyName':
                        $row[] = $array['targetName'];
                        break;
                    case 'money':
                        $money = find(array('table' => 'other_receipt_detail', 'where' => "orderId = '$array[id]'", 'sum' => "money"));
                        $row[] = $money;
                        break;
                    case 'stid':
                        $row[] = self::findStaff($array['stid']);
                        break;
                    case 'url':
                        $voucher = query('voucher', "id = '$array[voucherId]'");
                        $voucherWord = query('voucher_word', "id = '$voucher[voucherWordId]'");
                        $url = $array['voucherId'] ? (componentClass::urlTitle($voucherWord['word'] . '-' . $voucher['voucherNo'], root . 'control/adIndex.html?cla=voucherEdit&fun=room&id=' . $voucher['id'], '新增凭证')) : '';
                        $row[] = $url;
                        break;
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        $totalQuery = find([
            'table'  => "other_receipt_detail",
            'single' => " orderId in (select id from other_receipt where 1=1 {$where}) ",
            'field'  => "ifnull(sum(money),0) as money",
        ]);
        $total    = [
            'money'        => self::lostZero($totalQuery['money']),
        ];
        //生成页面
        $li = $this->li(array(
            "body" => $body,
            "install" => $installSearch,
            "button" => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page" => $page['para'],
            "total"       => $total,
        ));
        //返回
        return $page['hint'] .
            $this->buttonGroup() .
            "<form name='listForm'>" .
            space .
            hidden(array('name' => 'typeOne', 'value' => $oneMenuName)) .
            hidden(array('name' => 'typeTwo', 'value' => $secMenuName)) .
            $li .
            "</form>" .
            $this->js() .
            $page['button'];
    }


    /**
     * 费用单
     * @param $oneMenuName
     * @param $secMenuName
     * @return string
     */
    private function feiyongdan($oneMenuName, $secMenuName): string
    {
        //like查询数组
        $like = array(
            "orderNo" => $this->post('orderNo'),
        );
        $equal = [
            'coid' => self::$coid,
            'state' => '已确认',
            'target' => $this->post('type')
        ];
        $and = " and classify = 1";
        if ($this->post('createTime')) {
            $and .= self::timeComparison($this->post('createTime'), 'date', 'recordDate');
        }
        if ($this->post('khid')) {
            $and .= " and targetId = '" . $this->post('khid') . "'";
        }
        if ($this->post('supplierId')) {
            $and .= " and targetId = '" . $this->post('supplierId') . "'";
        }
        //获取查询结果
        $where = search([
                "like" => $like,
                '=' => $equal,
            ]) . $and;
        //排序
        $orderBy = $this->listSort(array(
            "search"  => $this->post('list_sort_key') ?: 'recordDate',
            "sort" => $this->post('list_sort') ?: "desc",
            "array"   => array(),
        ));
        $page = $this->page([
            "table" => 'other_receipt',
            "where" => $where,
            "order" => $orderBy['orderBy']
        ]);
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = array(
            $this->checkAll("listForm", "listId")
        );
        //获取记录
        foreach ($page['array'] as $array) {
            //列表控件
            $dominate = array(
                "box" => array("name" => "listId", "value" => $array['id']),
            );
            $liButton[] = $this->dominate($dominate);
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    case 'type':
                        $row[] = '费用单';
                        break;
                    case 'companyName':
                        $row[] = $array['targetName'];
                        break;
                    case 'money':
                        $money = find(array('table' => 'other_receipt_detail', 'where' => "orderId = '$array[id]'", 'sum' => "money"));
                        $row[] = $money;
                        break;
                    case 'stid':
                        $row[] = self::findStaff($array['stid']);
                        break;
                    case 'url':
                        $voucher = query('voucher', "id = '$array[voucherId]'");
                        $voucherWord = query('voucher_word', "id = '$voucher[voucherWordId]'");
                        $url = $array['voucherId'] ? (componentClass::urlTitle($voucherWord['word'] . '-' . $voucher['voucherNo'], root . 'control/adIndex.html?cla=voucherEdit&fun=room&id=' . $voucher['id'], '新增凭证')) : '';
                        $row[] = $url;
                        break;
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        $totalQuery = find([
            'table'  => "other_receipt_detail",
            'single' => " orderId in (select id from other_receipt where 1=1 {$where}) ",
            'field'  => "ifnull(sum(money),0) as money",
        ]);
        $total    = [
            'money'        => self::lostZero($totalQuery['money']),
        ];
        //生成页面
        $li = $this->li(array(
            "body" => $body,
            "install" => $installSearch,
            "button" => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page" => $page['para'],
            "total"       => $total,
        ));
        //返回
        return $page['hint'] .
            $this->buttonGroup() .
            "<form name='listForm'>" .
                space .
                hidden(array('name' => 'typeOne', 'value' => $oneMenuName)) .
                hidden(array('name' => 'typeTwo', 'value' => $secMenuName)) .
                $li .
            "</form>" .
            $this->js() .
            $page['button'];
    }


    /**
     * 付款单
     * @param $oneMenuName
     * @param $secMenuName
     * @return string
     */
    private function fukuandan($oneMenuName, $secMenuName): string
    {
        //like查询数组
        $like = array(
            "orderNo" => $this->post('orderNo'),
        );
        $equal = [
            "targetId" => $this->post('khid'),
            'coid' => self::$coid,
            'state' => '已确认',
        ];
        $and = " and classify = 0";
        if ($this->post('createTime')) {
            $and .= self::timeComparison($this->post('createTime'), 'date', 'recordDate');
        }
        //获取查询结果
        $where = search([
                "like" => $like,
                '=' => $equal,
            ]) . $and;
        //排序
        $orderBy = $this->listSort(array(
            "search"  => $this->post('list_sort_key') ?: 'recordDate',
            "sort" => $this->post('list_sort') ?: "desc",
            "array"   => array(),
        ));
        $page = $this->page([
            "table" => 'receipt',
            "where" => $where,
            "order" => $orderBy['orderBy']
        ]);
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = array(
            $this->checkAll("listForm", "listId")
        );
        //获取记录
        foreach ($page['array'] as $array) {
            //列表控件
            $dominate = array(
                "box" => array("name" => "listId", "value" => $array['id']),
            );
            $liButton[] = $this->dominate($dominate);
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    case 'type':
                        $row[] = '付款单';
                        break;
                    case 'companyName':
                        $kehu = query('kehu', "khid = '$array[targetId]'");
                        $row[] = $kehu['companyName'];
                        break;
                    case 'money':
                        $money = find(array('table' => 'receipt_detail', 'where' => "orderId = '$array[id]'", 'sum' => "money"));
                        $row[] = $money;
                        break;
                    case 'stid':
                        $row[] = self::findStaff($array['stid']);
                        break;
                    case 'url':
                        $voucher = query('voucher', "id = '$array[voucherId]'");
                        $voucherWord = query('voucher_word', "id = '$voucher[voucherWordId]'");
                        $url = $array['voucherId'] ? (componentClass::urlTitle($voucherWord['word'] . '-' . $voucher['voucherNo'], root . 'control/adIndex.html?cla=voucherEdit&fun=room&id=' . $voucher['id'], '新增凭证')) : '';
                        $row[] = $url;
                        break;
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        $totalQuery = find([
            'table'  => "receipt_detail",
            'single' => " orderId in (select id from receipt where 1=1 {$where}) ",
            'field'  => "ifnull(sum(money),0) as money",
        ]);
        $total    = [
            'money'        => self::lostZero($totalQuery['money']),
        ];
        //生成页面
        $li = $this->li(array(
            "body" => $body,
            "install" => $installSearch,
            "button" => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page" => $page['para'],
            "total"       => $total,
        ));
        //返回
        return $page['hint'] .
            $this->buttonGroup() .
            "<form name='listForm'>" .
            space .
            hidden(array('name' => 'typeOne', 'value' => $oneMenuName)) .
            hidden(array('name' => 'typeTwo', 'value' => $secMenuName)) .
            $li .
            "</form>" .
            $this->js() .
            $page['button'];
    }

    /**
     * 收款单
     * @param $oneMenuName
     * @param $secMenuName
     * @return string
     */
    private function shoukuandan($oneMenuName, $secMenuName): string
    {
        //like查询数组
        $like = array(
            "orderNo" => $this->post('orderNo'),
        );
        $equal = [
            "targetId" => $this->post('khid'),
            'coid' => self::$coid,
            'state' => '已确认',
            'classify' => 1
        ];
        $and = '';
        if ($this->post('createTime')) {
            $and .= self::timeComparison($this->post('createTime'), 'date', 'recordDate');
        }
        //获取查询结果
        $where = search([
                "like" => $like,
                '=' => $equal,
            ]) . $and;
        //排序
        $orderBy = $this->listSort(array(
            "search"  => $this->post('list_sort_key') ?: 'recordDate',
            "sort" => $this->post('list_sort') ?: "desc",
            "array"   => array(),
        ));
        $page = $this->page([
            "table" => 'receipt',
            "where" => $where,
            "order" => $orderBy['orderBy']
        ]);
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = array(
            $this->checkAll("listForm", "listId")
        );
        //获取记录
        foreach ($page['array'] as $array) {
            //列表控件
            $dominate = array(
                "box" => array("name" => "listId", "value" => $array['id']),
            );
            $liButton[] = $this->dominate($dominate);
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    case 'type':
                        $row[] = '收款单';
                        break;
                    case 'companyName':
                        $kehu = query('kehu', "khid = '$array[targetId]'");
                        $row[] = $kehu['companyName'];
                        break;
                    case 'money':
                        $money = find(array('table' => 'receipt_detail', 'where' => "orderId = '$array[id]'", 'sum' => "money"));
                        $row[] = $money;
                        break;
                    case 'stid':
                        $row[] = self::findStaff($array['stid']);
                        break;
                    case 'url':
                        $voucher = query('voucher', "id = '$array[voucherId]'");
                        $voucherWord = query('voucher_word', "id = '$voucher[voucherWordId]'");
                        $url = $array['voucherId'] ? (componentClass::urlTitle($voucherWord['word'] . '-' . $voucher['voucherNo'], root . 'control/adIndex.html?cla=voucherEdit&fun=room&id=' . $voucher['id'], '新增凭证')) : '';
                        $row[] = $url;
                        break;
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        $totalQuery = find([
            'table'  => "receipt_detail",
            'single' => " orderId in (select id from receipt where 1=1 {$where}) ",
            'field'  => "ifnull(sum(money),0) as money",
        ]);
        $total    = [
            'money'        => self::lostZero($totalQuery['money']),
        ];
        //生成页面
        $li = $this->li(array(
            "body" => $body,
            "install" => $installSearch,
            "button" => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page" => $page['para'],
            "total"       => $total,
        ));
        //返回
        return $page['hint'] .
            $this->buttonGroup() .
            "<form name='listForm'>" .
                space .
                hidden(array('name' => 'typeOne', 'value' => $oneMenuName)) .
                hidden(array('name' => 'typeTwo', 'value' => $secMenuName)) .
                $li .
            "</form>" .
            $this->js() .
            $page['button'];
    }

    /**
     * 采购暂估单
     * @param $oneMenuName
     * @param $secMenuName
     * @return string
     */
    private function caigouzangudan($oneMenuName, $secMenuName): string
    {
        //like查询数组
        $like = array(
            "orderNo" => $this->post('orderNo'),
        );
        $equal = [
            "supplierId" => $this->post('supplierId'),
            'coid' => self::$coid,
            'state' => '已确认'
        ];
        $and = '';
        if ($this->post('createTime')) {
            $and .= self::timeComparison($this->post('createTime'), 'date', 'recordDate');
        }
        //获取查询结果
        $where = search([
                "like" => $like,
                '=' => $equal,
            ]) . $and;
        //排序
        $orderBy = $this->listSort(array(
            "search"  => $this->post('list_sort_key') ?: 'recordDate',
            "sort" => $this->post('list_sort') ?: "desc",
            "array"   => array(),
        ));
        $page = $this->page([
            "table" => 'purchase_esti_order',
            "where" => $where,
            "order" => $orderBy['orderBy']
        ]);
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = array(
            $this->checkAll("listForm", "listId")
        );
        //获取记录
        foreach ($page['array'] as $array) {
            //列表控件
            $dominate = array(
                "box" => array("name" => "listId", "value" => $array['id']),
            );
            $liButton[] = $this->dominate($dominate);
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    case 'type':
                        $row[] = '采购暂估单';
                        break;
                    case 'companyName':
                        $supplier = query('supplier', "id = '$array[supplierId]'");
                        $row[] = $supplier['name'];
                        break;
                    case 'money':
                        $money = find(array('table' => 'purchase_esti_order_detail', 'where' => "orderId = '$array[id]'", 'sum' => "money"));
                        $row[] = $money;
                        break;
                    case 'stid':
                        $row[] = self::findStaff($array['stid']);
                        break;
                    case 'url':
                        $voucher = query('voucher', "id = '$array[voucherId]'");
                        $voucherWord = query('voucher_word', "id = '$voucher[voucherWordId]'");
                        $url = $array['voucherId'] ? (componentClass::urlTitle($voucherWord['word'] . '-' . $voucher['voucherNo'], root . 'control/adIndex.html?cla=voucherEdit&fun=room&id=' . $voucher['id'], '新增凭证')) : '';
                        $row[] = $url;
                        break;
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        $totalQuery = find([
            'table'  => "purchase_esti_order_detail",
            'single' => " orderId in (select id from purchase_esti_order where 1=1 {$where}) ",
            'field'  => "ifnull(sum(money),0) as money",
        ]);
        $total    = [
            'money'        => self::lostZero($totalQuery['money']),
        ];
        //生成页面
        $li = $this->li(array(
            "body" => $body,
            "install" => $installSearch,
            "button" => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page" => $page['para'],
            "total"       => $total,
        ));
        //返回
        return $page['hint'] .
            $this->buttonGroup() .
            "<form name='listForm'>" .
                space .
                hidden(array('name' => 'typeOne', 'value' => $oneMenuName)) .
                hidden(array('name' => 'typeTwo', 'value' => $secMenuName)) .
                $li .
            "</form>" .
            $this->js() .
            $page['button'];
    }

    /**
     * 采购单
     * @param $oneMenuName
     * @param $secMenuName
     * @return string
     */
    private function caigoudan($oneMenuName, $secMenuName): string
    {
        if ($secMenuName == '采购入库') {
            return $this->caigouruku($oneMenuName, $secMenuName);
        } elseif ($secMenuName == '采购退货') {
            return $this->caigoutuihuo($oneMenuName, $secMenuName);
        } else {
            return '未找到类型';
        }
    }

    /**
     * 销售单
     * @param $oneMenuName
     * @param $secMenuName
     * @return string
     */
    private function xiaoshoudan($oneMenuName, $secMenuName): string
    {
        if ($secMenuName == '销售出库') {
            return $this->xiaoshouchuku($oneMenuName, $secMenuName);
        } elseif ($secMenuName == '销售退货') {
            return $this->xiaoshoutuihuo($oneMenuName, $secMenuName);
        } else {
            return '未找到类型';
        }
    }

    /**
     * 采购退货
     * @param $oneMenuName
     * @param $secMenuName
     * @return string
     */
    private function caigoutuihuo($oneMenuName, $secMenuName)
    {
        //like查询数组
        $like = array(
            "orderNo" => $this->post('orderNo'),
        );
        $equal = [
            "supplierId" => $this->post('supplierId'),
            'coid' => self::$coid,
            'state' => '已确认'
        ];
        $and = '';
        if ($this->post('createTime')) {
            $and .= self::timeComparison($this->post('createTime'), 'date', 'recordDate');
        }
        //获取查询结果
        $where = search([
                "like" => $like,
                '=' => $equal,
            ]) . $and;
        //排序
        $orderBy = $this->listSort(array(
            "search"  => $this->post('list_sort_key') ?: 'recordDate',
            "sort" => $this->post('list_sort') ?: "desc",
            "array"   => array(),
        ));
        $page = $this->page([
            "table" => 'purchase_refund_order',
            "where" => $where,
            "order" => $orderBy['orderBy']
        ]);
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = array(
            $this->checkAll("listForm", "listId")
        );
        //获取记录
        foreach ($page['array'] as $array) {
            //列表控件
            $dominate = array(
                "box" => array("name" => "listId", "value" => $array['id']),
            );
            $liButton[] = $this->dominate($dominate);
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    case 'type':
                        $row[] = '采购退货单';
                        break;
                    case 'companyName':
                        $supplier = query('supplier', "id = '$array[supplierId]'");
                        $row[] = $supplier['name'];
                        break;
                    case 'money':
                        $money = find(array('table' => 'purchase_refund_order_detail', 'where' => "orderId = '$array[id]'", 'sum' => "money"));
                        $row[] = $money;
                        break;
                    case 'stid':
                        $row[] = self::findStaff($array['stid']);
                        break;
                    case 'url':
                        $voucher = query('voucher', "id = '$array[voucherId]'");
                        $voucherWord = query('voucher_word', "id = '$voucher[voucherWordId]'");
                        $url = $array['voucherId'] ? (componentClass::urlTitle($voucherWord['word'] . '-' . $array['voucherNo'], root . 'control/adIndex.html?cla=voucherEdit&fun=room&id=' . $voucher['id'], '新增凭证')) : '';
                        $row[] = $url;
                        break;
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        $totalQuery = find([
            'table'  => "purchase_refund_order_detail",
            'single' => " orderId in (select id from purchase_refund_order where 1=1 {$where}) ",
            'field'  => "ifnull(sum(money),0) as money",
        ]);
        $total    = [
            'money'        => self::lostZero($totalQuery['money']),
        ];
        //生成页面
        $li = $this->li(array(
            "body" => $body,
            "install" => $installSearch,
            "button" => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page" => $page['para'],
            "total"       => $total,
        ));
        //返回
        return $page['hint'] .
            $this->buttonGroup() .
            "<form name='listForm'>" .
                space .
                hidden(array('name' => 'typeOne', 'value' => $oneMenuName)) .
                hidden(array('name' => 'typeTwo', 'value' => $secMenuName)) .
                $li .
            "</form>" .
            $this->js() .
            $page['button'];
    }

    /**
     * 采购入库
     * @param $oneMenuName
     * @param $secMenuName
     * @return string
     */
    private function caigouruku($oneMenuName, $secMenuName)
    {
        //like查询数组
        $like = array(
            "orderNo" => $this->post('orderNo'),
        );
        $equal = [
            "supplierId" => $this->post('supplierId'),
            'coid' => self::$coid,
            'state' => '已确认'
        ];
        $and = '';
        if ($this->post('createTime')) {
            $and .= self::timeComparison($this->post('createTime'), 'date', 'recordDate');
        }
        //获取查询结果
        $where = search([
                "like" => $like,
                '=' => $equal,
            ]) . $and;
        //排序
        $orderBy = $this->listSort(array(
            "search"  => $this->post('list_sort_key') ?: 'recordDate',
            "sort" => $this->post('list_sort') ?: "desc",
            "array"   => array(),
        ));
        $page = $this->page([
            "table" => 'purchase_get_order',
            "where" => $where,
            "order" => $orderBy['orderBy']
        ]);
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = array(
            $this->checkAll("listForm", "listId")
        );
        //获取记录
        foreach ($page['array'] as $array) {
            //列表控件
            $dominate = array(
                "box" => array("name" => "listId", "value" => $array['id']),
            );
            $liButton[] = $this->dominate($dominate);
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    case 'type':
                        $row[] = '采购收货单';
                        break;
                    case 'companyName':
                        $supplier = query('supplier', "id = '$array[supplierId]'");
                        $row[] = $supplier['name'];
                        break;
                    case 'money':
                        $money = find(array('table' => 'purchase_get_order_detail', 'where' => "orderId = '$array[id]'", 'sum' => "money"));
                        $row[] = $money;
                        break;
                    case 'stid':
                        $row[] = self::findStaff($array['stid']);
                        break;
                    case 'url':
                        $voucher = query('voucher', "id = '$array[voucherId]'");
                        $voucherWord = query('voucher_word', "id = '$voucher[voucherWordId]'");
                        $url = $array['voucherId'] ? (componentClass::urlTitle($voucherWord['word'] . '-' . $voucher['voucherNo'], root . 'control/adIndex.html?cla=voucherEdit&fun=room&id=' . $voucher['id'], '新增凭证')) : '';
                        $row[] = $url;
                        break;
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        $totalQuery = find([
            'table'  => 'purchase_get_order_detail',
            'single' => " orderId in (select id from purchase_get_order where 1=1 {$where}) ",
            'field'  => "ifnull(sum(money),0) as money",
        ]);
        $total    = [
            'money'        => self::lostZero($totalQuery['money']),
        ];
        //生成页面
        $li = $this->li(array(
            "body" => $body,
            "install" => $installSearch,
            "button" => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page" => $page['para'],
            "total"       => $total,
        ));
        //返回
        return $page['hint'] .
            $this->buttonGroup() .
            "<form name='listForm'>" .
            space .
            hidden(array('name' => 'typeOne', 'value' => $oneMenuName)) .
            hidden(array('name' => 'typeTwo', 'value' => $secMenuName)) .
            $li .
            "</form>" .
            $this->js() .
            $page['button'];
    }

    private function xiaoshouchuku($oneMenuName, $secMenuName)
    {
        //like查询数组
        $like = array(
            "orderNo" => $this->post('orderNo'),
        );
        $equal = [
            "khid" => $this->post('khid'),
            'coid' => self::$coid,
            'state' => '已确认'
        ];
        $and = '';
        if ($this->post('createTime')) {
            $and .= self::timeComparison($this->post('createTime'), 'date', 'recordDate');
        }
        //获取查询结果
        $where = search([
                "like" => $like,
                '=' => $equal,
            ]) . $and;
        //排序
        $orderBy = $this->listSort(array(
            "search"  => $this->post('list_sort_key') ?: 'recordDate',
            "sort" => $this->post('list_sort') ?: "desc",
            "array"   => array(),
        ));
        $page = $this->page([
            "table" => 'sale_send_order',
            "where" => $where,
            "order" => $orderBy['orderBy']
        ]);
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = array(
            $this->checkAll("listForm", "listId")
        );
        //获取记录
        foreach ($page['array'] as $array) {
            //列表控件
            $dominate = array(
                "box" => array("name" => "listId", "value" => $array['id']),
            );
            $liButton[] = $this->dominate($dominate);
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    case 'type':
                        $row[] = '销售发货单';
                        break;
                    case 'companyName':
                        $kehu = query('kehu', "khid = '$array[khid]'");
                        $row[] = $kehu['companyName'];
                        break;
                    case 'money':
                        $money = find(array('table' => 'sale_send_order_detail', 'where' => "orderId = '$array[id]'", 'sum' => "money"));
                        $row[] = $money;
                        break;
                    case 'stid':
                        $row[] = self::findStaff($array['stid']);
                        break;
                    case 'url':
                        $voucher = query('voucher', "id = '$array[voucherId]'");
                        $voucherWord = query('voucher_word', "id = '$voucher[voucherWordId]'");
                        $url = $array['voucherId'] ? (componentClass::urlTitle($voucherWord['word'] . '-' . $voucher['voucherNo'], root . 'control/adIndex.html?cla=voucherEdit&fun=room&id=' . $voucher['id'], '新增凭证')) : '';
                        $row[] = $url;
                        break;
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        $totalQuery = find([
            'table'  => 'sale_send_order_detail',
            'single' => " orderId in (select id from sale_send_order where 1=1 {$where}) ",
            'field'  => "ifnull(sum(money),0) as money",
        ]);
        $total    = [
            'money'        => self::lostZero($totalQuery['money']),
        ];
        //生成页面
        $li = $this->li(array(
            "body" => $body,
            "install" => $installSearch,
            "button" => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page" => $page['para'],
            "total"       => $total,
        ));
        //返回
        return $page['hint'] .
            $this->buttonGroup() .
            "<form name='listForm'>" .
            space .
            hidden(array('name' => 'typeOne', 'value' => $oneMenuName)) .
            hidden(array('name' => 'typeTwo', 'value' => $secMenuName)) .
            $li .
            "</form>" .
            $this->js() .
            $page['button'];
    }

    private function xiaoshoutuihuo($oneMenuName, $secMenuName)
    {
        //like查询数组
        $like = array(
            "orderNo" => $this->post('orderNo'),
        );
        $equal = [
            "khid" => $this->post('khid'),
            'coid' => self::$coid,
            'state' => '已确认'
        ];
        $and = '';
        if ($this->post('createTime')) {
            $and .= self::timeComparison($this->post('createTime'), 'date', 'recordDate');
        }
        //获取查询结果
        $where = search([
                "like" => $like,
                '=' => $equal,
            ]) . $and;
        //排序
        $orderBy = $this->listSort(array(
            "search"  => $this->post('list_sort_key') ?: 'recordDate',
            "sort" => $this->post('list_sort') ?: "desc",
            "array"   => array(),
        ));
        $page = $this->page([
            "table" => 'sale_refund_order',
            "where" => $where,
            "order" => $orderBy['orderBy']
        ]);
        //获取自定义字段参数
        $installSearch = $this->installSearch();
        $body          = [];
        //表格控件区
        $liButton = array(
            $this->checkAll("listForm", "listId")
        );
        //获取记录
        foreach ($page['array'] as $array) {
            //列表控件
            $dominate = array(
                "box" => array("name" => "listId", "value" => $array['id']),
            );
            $liButton[] = $this->dominate($dominate);
            //本行初始化
            $row = [];
            foreach ($installSearch['key'] as $key) {
                switch ($key) {
                    case 'type':
                        $row[] = '销售退货单';
                        break;
                    case 'companyName':
                        $kehu = query('kehu', "khid = '$array[khid]'");
                        $row[] = $kehu['companyName'];
                        break;
                    case 'money':
                        $money = find(array('table' => 'sale_refund_order_detail', 'where' => "orderId = '$array[id]'", 'sum' => "money"));
                        $row[] = $money;
                        break;
                    case 'stid':
                        $row[] = self::findStaff($array['stid']);
                        break;
                    case 'url':
                        $voucher = query('voucher', "id = '$array[voucherId]'");
                        $voucherWord = query('voucher_word', "id = '$voucher[voucherWordId]'");
                        $url = $array['voucherId'] ? (componentClass::urlTitle($voucherWord['word'] . '-' . $voucher['voucherNo'], root . 'control/adIndex.html?cla=voucherEdit&fun=room&id=' . $voucher['id'], '新增凭证')) : '';
                        $row[] = $url;
                        break;
                    default:
                        $row[] = isset($array[$key]) && $array[$key] ? $array[$key] : '';
                }
            }
            $body[] = $row;
        }
        $totalQuery = find([
            'table'  => 'sale_refund_order_detail',
            'single' => " orderId in (select id from sale_refund_order where 1=1 {$where}) ",
            'field'  => "ifnull(sum(money),0) as money",
        ]);
        $total    = [
            'money'        => self::lostZero($totalQuery['money']),
        ];
        //生成页面
        $li = $this->li(array(
            "body" => $body,
            "install" => $installSearch,
            "button" => array("html" => $liButton),
            "orderBySign" => $orderBy,
            "page" => $page['para'],
            "total"       => $total,
        ));
        //返回
        return $page['hint'] .
            $this->buttonGroup() .
            "<form name='listForm'>" .
                space .
                hidden(array('name' => 'typeOne', 'value' => $oneMenuName)) .
                hidden(array('name' => 'typeTwo', 'value' => $secMenuName)) .
                $li .
            "</form>" .
            $this->js() .
            $page['button'];
    }

    public function js()
    {
        $cla = self::page;
        return <<<eof
<script>
function voucher(){
    let arr = [];
    $("[name='listForm'] input[type=checkbox]:checked").each(function () {
        if ($(this).val() && $(this).val() !== 'on') {
            arr.push($(this).val());
        }
    });
    if (arr.length > 0) {
        let vals = arr.join(",");
        let typeOne = $("[name='typeOne']").val();
        let typeTwo = $("[name='typeTwo']").val();
        newIframe('业务凭证',root+'control/adIndex.html?cla=$cla&fun=generateVoucher&listId='+vals+'&typeOne='+typeOne+'&typeTwo='+typeTwo)
    } else {
        msg('请选择需要数据','error');
    }
}
</script>
eof;

    }

    // 操作中转
    public function operate(): bool|string
    {
        $act = $this->post('act') ?? '';
        $pwd = false; // 需要密码
        switch ($act) {
            default:
                $act = 'del';
                $msg = '删除' . self::title;
                $title = self::page . 'Delete';
                $function = 'delete';
                $form = self::page . 'OperateForm, listForm';
                $power = '';
                break;
        }
        if (!$this->power(self::page, $act)) {
            $_SESSION['warn'] = '你还没有' . $msg . '的权限';
            $json = [
                'warn' => 2,
                'href' => root . 'control/adIndex.html?cla=' . $this->get('cla') . '&fun=home',
            ];
        } else {
            return $this->confirm([
                'title' => $title,
                'function' => $function,
                'form' => $form,
                'power' => $power,
                'pwd' => $pwd,
            ]);
        }

        resp:
        return json_encode($json, 320);
    }


    /**
     * 删除
     * @return bool|string
     */
    public function delete()
    {
        $array = $this->post('listId') ?? $this->post('id');
        $typeTwo = $this->post('typeTwo');
        $typeOne = $this->post('typeOne');
        if (!$this->power(self::page, "del")) {
            $json['warn'] = "你没有删除的权限";
        } elseif (empty($array)) {
            $json['warn'] = "你一个数据都没有选择呢";
        } elseif (empty($typeOne) || empty($typeTwo)) {
            $json['warn'] = '未找到类别';
        } else {
            $text = "";
            $x = 0;
            $array = is_array($array) ? $array : [$array];
            foreach ($array as $id) {
                switch ($typeTwo) {
//                    case '领料单':
//                    case '退料单':
//                        $info = query('pick_order', "id = '$id'");
//                        update(array('table' => 'pick_order', 'field' => ['voucherId' => ''], 'where' => "id = '$info[id]'"));
//                        break;
                    case '采购入库':
                        $info = query('purchase_get_order', "id = '$id'");
                        update(array('table' => 'purchase_get_order', 'field' => ['voucherId' => ''], 'where' => "id = '$info[id]'"));
                        break;
                    case '采购退货':
                        $info = query('purchase_refund_order', "id = '$id'");
                        update(array('table' => 'purchase_refund_order', 'field' => ['voucherId' => ''], 'where' => "id = '$info[id]'"));
                        break;
                    case '销售出库':
                        $info = query('sale_send_order', "id = '$id'");
                        update(array('table' => 'sale_send_order', 'field' => ['voucherId' => ''], 'where' => "id = '$info[id]'"));
                        break;
                    case '销售退货':
                        $info = query('sale_refund_order', "id = '$id'");
                        update(array('table' => 'sale_refund_order', 'field' => ['voucherId' => ''], 'where' => "id = '$info[id]'"));
                        break;
                    case '采购暂估单':
                        $info = query('purchase_esti_order', "id = '$id'");
                        update(array('table' => 'purchase_esti_order', 'field' => ['voucherId' => ''], 'where' => "id = '$info[id]'"));
                        break;
                    case '收入单':
                    case '费用单':
                        $info = query('other_receipt', "id = '$id'");
                        update(array('table' => 'other_receipt', 'field' => ['voucherId' => ''], 'where' => "id = '$info[id]'"));
                        break;
                    case '收款单':
                    case '付款单':
                        $info = query('receipt', "id = '$id'");
                        update(array('table' => 'receipt', 'field' => ['voucherId' => ''], 'where' => "id = '$info[id]'"));
                        break;
                    case '销项发票':
                    case '进项发票':
                        $info = query('invoice_open', "id = '$id'");
                        $info['orderNo'] = $info['invoiceNo'];
                        update(array('table' => 'invoice_open', 'field' => ['voucherId' => ''], 'where' => "id = '$info[id]'"));
                        break;
                    case '领料单':
                    case '退料单':
                    case '生产入库单':
                    case '出库':
                    case '退货':
                    case '盘盈':
                    case '其他入库':
                    case '盘亏':
                    case '其他出库':
                        $info = query('inventory_changes', "id = '$id'");
                        update(array('table' => 'inventory_changes', 'field' => ['voucherId' => ''], 'where' => "id = '$info[id]'"));
                        break;
                    case '成本调整单':
                        $info = query('inventory_cost', "id = '$id'");
                        update(array('table' => 'inventory_cost', 'field' => ['voucherId' => ''], 'where' => "id = '$info[id]'"));
                        break;
                    case '组装单':
                        $info = query('inventory_assembly', "id = '$id'");
                        update(array('table' => 'inventory_assembly', 'field' => ['voucherId' => ''], 'where' => "id = '$info[id]'"));
                        break;
                    case '拆卸单':
                        $info = query('inventory_dismantle', "id = '$id'");
                        update(array('table' => 'inventory_dismantle', 'field' => ['voucherId' => ''], 'where' => "id = '$info[id]'"));
                        break;
                    case '转账单':
                        $info = query('transfer_order', "id = '$id'");
                        update(array('table' => 'transfer_order', 'field' => ['voucherId' => ''], 'where' => "id = '$info[id]'"));
                        break;
                    default:
                        if ($typeOne == '费用报销单') {
                            $info = query('cost', "id = '$id'");
                            update(array('table' => 'cost', 'field' => ['voucherId' => ''], 'where' => "id = '$info[id]'"));
                        } elseif ($typeOne == '核销单') {
                            $info = query('write_off_order_detail', "id = '$id'");
                            update(array('table' => 'write_off_order_detail', 'field' => ['voucherId' => ''], 'where' => "id = '$info[id]'"));
                        } else {
                            $info = [
                                'orderNo' => '',
                            ];
                        }

                        break;
                }

                //最后删除基本参数
                delete(array("table" => 'voucher', "where" => " id = '$info[voucherId]' "));
                delete(array("table" => 'voucher_detail', "where" => " orderId = '$info[voucherId]'"));
                //返回
                $warn = "删除成功";
                $x++;
                $text .= self::title . "ID【" . $info['id'] . "】，单据编号【" . $info['orderNo'] . "】，业务类型：【{$typeOne}】" . $warn . "。<br>";
            }
            //添加日志
            $text = $this->control['name'] . "删除了" . self::title . "，结果如下：<br>" . $text;
            $this->logText(array("type" => self::page, "text" => $text));
            $_SESSION['warn'] = "删除了" . $x . "个" . self::title . "，详情见系统日志。";
            $json['warn'] = 2;
            $json['href'] = root . "control/adIndex.html?cla=" . $this->get('cla') . "&fun=home&id=".$typeTwo;
        }
        return json_encode($json, 320);
    }

}
